提交 1dce8d1c 编写于 作者: Q Qiao Longfei 提交者: GitHub

Merge pull request #70 from ChunweiYan/feature/glog_to_exception

feature/glog to exception
......@@ -41,6 +41,7 @@ add_executable(vl_test
${PROJECT_SOURCE_DIR}/visualdl/logic/histogram_test.cc
${PROJECT_SOURCE_DIR}/visualdl/storage/storage_test.cc
${PROJECT_SOURCE_DIR}/visualdl/utils/test_concurrency.cc
${PROJECT_SOURCE_DIR}/visualdl/utils/test_logging.cc
${PROJECT_SOURCE_DIR}/visualdl/utils/test_image.cc
${PROJECT_SOURCE_DIR}/visualdl/utils/concurrency.h
${PROJECT_SOURCE_DIR}/visualdl/utils/filesystem.h
......
#ifndef VISUALDL_LOGIC_HISTOGRAM_H
#define VISUALDL_LOGIC_HISTOGRAM_H
#include <glog/logging.h>
#include <cstdlib>
#include <limits>
#include <vector>
#include "visualdl/utils/logging.h"
namespace visualdl {
// An interface to retrieve records of a histogram.
......
#include <glog/logging.h>
#include <ctime>
#include "visualdl/logic/im.h"
......
#ifndef VISUALDL_LOGIC_IM_H
#define VISUALDL_LOGIC_IM_H
#include <glog/logging.h>
#include <memory>
#include <mutex>
#include <string>
#include "visualdl/utils/concurrency.h"
#include "visualdl/utils/guard.h"
#include "visualdl/utils/logging.h"
namespace visualdl {
......
#ifndef VISUALDL_STORAGE_STORAGE_H
#define VISUALDL_STORAGE_STORAGE_H
#include <glog/logging.h>
#include <algorithm>
#include <set>
#include <vector>
......
#ifndef VISUALDL_TABLET_H
#define VISUALDL_TABLET_H
#include <glog/logging.h>
#include "visualdl/logic/im.h"
#include "visualdl/storage/record.h"
#include "visualdl/storage/storage.pb.h"
......
#include "gtest/gtest.h"
#include "visualdl/utils/logging.h"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
std::signal(SIGINT, visualdl::log::SignalHandler);
return RUN_ALL_TESTS();
}
\ No newline at end of file
}
#ifndef VISUALDL_UTILS_CONCURRENCY_H
#define VISUALDL_UTILS_CONCURRENCY_H
#include <glog/logging.h>
#include <chrono>
#include <memory>
#include <thread>
#include <vector>
#include "visualdl/utils/logging.h"
namespace visualdl {
namespace cc {
......
......@@ -44,7 +44,6 @@ bool DeSerializeFromFile(T* proto, const std::string& path) {
}
static void TryMkdir(const std::string& dir) {
VLOG(1) << "try to mkdir " << dir;
struct stat st = {0};
if (stat(dir.c_str(), &st) == -1) {
::mkdir(dir.c_str(), 0700);
......@@ -67,7 +66,6 @@ static void TryRecurMkdir(const std::string& path) {
inline void Write(const std::string& path,
const std::string& buffer,
std::ios::openmode open_mode = std::ios::binary) {
VLOG(1) << "write to path " << path;
std::ofstream file(path, open_mode);
CHECK(file.is_open()) << "failed to open " << path;
file.write(buffer.c_str(), buffer.size());
......@@ -76,7 +74,6 @@ inline void Write(const std::string& path,
inline std::string Read(const std::string& path,
std::ios::openmode open_mode = std::ios::binary) {
VLOG(1) << "read from path " << path;
std::string buffer;
std::ifstream file(path, open_mode | std::ios::ate);
CHECK(file.is_open()) << "failed to open " << path;
......
#ifndef VISUALDL_UTILS_IMAGE_H
#define VISUALDL_UTILS_IMAGE_H
#include <glog/logging.h>
#include <Eigen/Core>
#include <unsupported/Eigen/CXX11/Tensor>
#include "visualdl/utils/logging.h"
namespace visualdl {
using uint8_t = unsigned char;
......
#ifndef VISUALDL_UTILS_LOG_H
#define VISUALDL_UTILS_LOG_H
#include <stdexcept>
namespace visualdl {
namespace log {
class NotImplementedException : public std::logic_error {
public:
NotImplementedException() : std::logic_error{"Function not implemented"} {}
};
} // namespace log
} // namespace visualdl
#endif
#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 : public std::basic_ostream<char, std::char_traits<char>> {
LogStreamFatal(const char* file, int line) {
ss << "[" << file << ":" << line << "] ";
throw Error(ss.str());
}
LogStreamFatal& operator<<(std::basic_streambuf<char, std::char_traits<char>>* sb) {
*this << sb;
if (sb->sgetc() == '\n') {
std::cout << "end get";
}
return *this;
}
friend LogStreamFatal& operator<<(LogStreamFatal& os,
const char* msg) {
std::cout << "msg: '" << msg << "'" << std::endl;
os.stream() << msg;
if (msg == "\n") {
os.has_throw_ = true;
throw Error(os.stream().str());
}
return os;
}
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__)
// basic version without support for debug level.
#define VLOG(x) LOG_INFO
#define CHECK(cond) \
if (!(cond)) \
visualdl::logging::LogStreamFatal(__FILE__, __LINE__) \
<< "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
#include "visualdl/utils/concurrency.h"
#include <glog/logging.h>
#include <gtest/gtest.h>
#include "visualdl/utils/logging.h"
namespace visualdl {
int counter = 0;
......
#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" << "\n";
}
// throw std::runtime_error("some error");
} catch (const visualdl::logging::Error& e) {
LOG(INFO) << "in exception";
} catch (...) {
LOG(INFO) << "catch something";
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册