Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/glog to exception #73

Merged
merged 10 commits into from
Jan 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ cache:
- npm
sudo: required
dist: trusty

os:
- linux
# much bug with osx environment
# TODO(ChunweiYan) support osx in the future
#- osx

addons:
apt:
packages:
Expand All @@ -23,6 +28,11 @@ addons:
- npm
- nodejs

before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew upgrade python; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install brew-pip; fi

script:
/bin/bash ./tests.sh all

Expand Down
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(THIRD_PARTY_PATH "${CMAKE_BINARY_DIR}/third_party" CACHE STRING

################################ Configurations #######################################
option(WITH_TESTING "Compile VisualDL with unit testing" ON)
option(ON_RELEASE "RELEASE mode" ON)


include(external/zlib) # download, build, install zlib
Expand All @@ -28,13 +29,20 @@ include(external/pybind11) # download pybind11
include(external/protobuf) # download, build, install protobuf
include(external/python) # find python and set path


if (NOT ON_RELEASE)
message(STATUS "build on debug mode")
add_compile_options(-DVISUALDL_WITH_GLOG)
endif(NOT ON_RELEASE)

include_directories(${PROJECT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/visualdl/storage)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/visualdl/logic)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/visualdl/python)

if (NOT ON_RELEASE)
add_executable(vl_test
${PROJECT_SOURCE_DIR}/visualdl/test.cc
${PROJECT_SOURCE_DIR}/visualdl/logic/sdk_test.cc
Expand All @@ -46,8 +54,6 @@ add_executable(vl_test
${PROJECT_SOURCE_DIR}/visualdl/utils/filesystem.h
)
target_link_libraries(vl_test sdk storage entry tablet im gtest glog protobuf gflags pthread eigen3)

enable_testing ()

add_custom_target(test_init COMMAND $CMAKE_BINARY_DIR)
add_test(NAME vstest COMMAND ./vl_test)
endif(NOT ON_RELEASE)
29 changes: 19 additions & 10 deletions tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ readonly core_path=$TOP_DIR/build/visualdl/logic
readonly python_path=$TOP_DIR/visualdl/python
readonly max_file_size=1000000 # 1MB

sudo="sudo"

if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then sudo=""; fi

if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
curl -O http://python-distribute.org/distribute_setup.py
python distribute_setup.py
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
python get-pip.py
fi

$sudo pip install numpy
$sudo pip install Flask
$sudo pip install Pillow
$sudo pip install protobuf

export PYTHONPATH="${core_path}:${python_path}"

# install the visualdl wheel first
Expand All @@ -22,21 +38,14 @@ package() {
export PATH="$PATH:$(pwd)/protoc3/bin"
chmod +x protoc3/bin/*

sudo pip install numpy
sudo pip install Flask
sudo pip install Pillow
sudo pip install protobuf

#sudo apt-get install protobuf-compiler
cd $TOP_DIR
python setup.py bdist_wheel
sudo pip install dist/visualdl-0.0.1-py2-none-any.whl
$sudo pip install dist/visualdl-0.0.1-py2-none-any.whl
}

backend_test() {
cd $TOP_DIR
sudo pip install numpy
sudo pip install Pillow
mkdir -p build
cd build
cmake ..
Expand All @@ -52,8 +61,8 @@ frontend_test() {
}

server_test() {
sudo pip install google
sudo pip install protobuf==3.1.0
$sudo pip install google
$sudo pip install protobuf==3.1.0

cd $TOP_DIR/visualdl/server
bash graph_test.sh
Expand Down
19 changes: 0 additions & 19 deletions visualdl/utils/log.h

This file was deleted.

126 changes: 126 additions & 0 deletions visualdl/utils/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#ifndef VISUALDL_UTILS_LOGGING_H
#define VISUALDL_UTILS_LOGGING_H

#include <csignal>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>

#if defined(VISUALDL_WITH_GLOG)
#include <glog/logging.h>
#endif

namespace visualdl {
namespace logging {
#if !defined(VISUALDL_WITH_GLOG)

// basic log stream for INFO, ERROR, WARNING
struct LogStream {
LogStream(const char* file, int line) : os_(std::cerr) {
os_ << "[" << file << ":" << line << "] ";
}

~LogStream() { os_ << "\n"; }

std::ostream& stream() { return os_; }

void operator=(const LogStream&) = delete;
LogStream(const LogStream&) = delete;

private:
std::ostream& os_;
};
#endif

#if !defined(VISUALDL_WITH_GLOG)
#if defined(VISUALDL_FATAL_ABORT)
// log stream for FATAL
struct LogStreamFatal : public LogStream {
LogStreamFatal(const char* file, int line) : LogStream(file, line) {}
~LogStreamFatal() { abort(); }

LogStreamFatal(const LogStreamFatal&) = delete;
void operator=(const LogStreamFatal&) = delete;
};

#else

struct Error : public std::runtime_error {
explicit Error(const std::string& s) : std::runtime_error(s) {}
};

// With exception.
struct LogStreamFatal {
LogStreamFatal(const char* file, int line) {
ss << "[" << file << ":" << line << "] ";
throw Error(ss.str());
}

std::stringstream& stream() { return ss; }

~LogStreamFatal() {
if (!has_throw_) {
std::cerr << "throw exception" << std::endl;
throw Error(ss.str());
}
}

private:
bool has_throw_{false};
mutable std::stringstream ss;
};

#endif // VISUALDL_FATAL_ABORT
#endif // VISUALDL_WITH_GLOG

#ifndef VISUALDL_WITH_GLOG

#define LOG(severity) LOG_##severity
#define LOG_INFO visualdl::logging::LogStream(__FILE__, __LINE__).stream()
#define LOG_WARNING LOG_INFO
#define LOG_ERROR LOG_INFO
#define LOG_FATAL visualdl::logging::LogStreamFatal(__FILE__, __LINE__).stream()
// basic version without support for debug level.
#define VLOG(x) LOG_INFO

#define CHECK(cond) \
if (!(cond)) \
visualdl::logging::LogStreamFatal(__FILE__, __LINE__).stream() \
<< "Check failed: " << #cond << " "
#define CHECK_EQ(v0, v1) CHECK_BINARY(v0, v1, ==)
#define CHECK_GE(v0, v1) CHECK_BINARY(v0, v1, >=)
#define CHECK_GT(v0, v1) CHECK_BINARY(v0, v1, >)
#define CHECK_LE(v0, v1) CHECK_BINARY(v0, v1, <=)
#define CHECK_LT(v0, v1) CHECK_BINARY(v0, v1, <)
#define CHECK_BINARY(v0, v1, op) \
if (!(v0 op v1)) LOG_FATAL << " Check failed: " << v0 << #op << v1 << " "

#endif // ifndef VISUALDL_WITH_GLOG

} // namespace logging
} // namespace visualdl

namespace visualdl {

namespace log {

class NotImplementedException : public std::logic_error {
public:
NotImplementedException() : std::logic_error{"Function not implemented"} {}
};

static void SignalHandler(int sig) {
LOG(INFO) << "get signal " << sig;
exit(sig);
}

struct __once_caller__ {
__once_caller__() { std::signal(SIGINT, SignalHandler); }
};

} // namespace log

} // namespace visualdl

#endif
23 changes: 23 additions & 0 deletions visualdl/utils/test_logging.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifdef VISUALDL_WITH_GLOG
#undef VISUALDL_WITH_GLOG
#endif
#include "visualdl/utils/logging.h"

#include <gtest/gtest.h>

TEST(Log, LOG) { LOG(INFO) << "hello world"; }

TEST(Log, CHECK) { CHECK_EQ(1, 1) << "yes this works"; }
TEST(Log, CHECK_FAIL) {
try {
CHECK_LE(3, 2) << "this is wrong";
// throw std::runtime_error("some error");
} catch (const visualdl::logging::Error& e) {
LOG(INFO) << e.what();
auto msg = std::string(e.what());
auto it = msg.find("test_logging.cc:14");
ASSERT_GT(it, 0);
} catch (...) {
LOG(INFO) << "catch something";
}
}