Last active
July 9, 2017 08:33
-
-
Save motoishmz/d3e57a4df1851e6ff645668a92bf401c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "log.h" | |
#include "ofUtils.h" | |
namespace aoi { | |
namespace detail { | |
static std::stack<ofLogLevel> log_level_stack; | |
static std::mutex log_level_mutex; | |
} | |
#pragma mark - | |
void PushLogLevel() { | |
std::lock_guard<std::mutex> lock(detail::log_level_mutex); | |
detail::log_level_stack.push(ofGetLogLevel()); | |
} | |
void PopLogLevel() { | |
std::lock_guard<std::mutex> lock(detail::log_level_mutex); | |
if (detail::log_level_stack.empty()) { | |
ofLogError("aoi::PopLogLevel") << "Unbalanced call! did nothing."; | |
} else { | |
ofSetLogLevel(detail::log_level_stack.top()); | |
detail::log_level_stack.pop(); | |
} | |
} | |
#pragma mark - | |
LoggerChannel::LoggerChannel() | |
: should_save_to_file_(false) | |
{} | |
LoggerChannel::~LoggerChannel() { | |
file_.close(); | |
} | |
void LoggerChannel::setup(std::string path, bool append) { | |
file_.open(path, append ? ofFile::Append : ofFile::WriteOnly); | |
file_ << endl; | |
file_ << endl; | |
file_ << "----------- " << ofGetTimestampString() << endl; | |
should_save_to_file_ = true; | |
} | |
void LoggerChannel::log(ofLogLevel level, | |
const string & module, | |
const string & message) { | |
ostream & out = level < OF_LOG_ERROR ? cout : cerr; | |
out << ofGetTimestampString() << " "; | |
out << "[" << ofGetLogLevelName(level, true) << "] "; | |
if(!module.empty()) { | |
out << module << ": "; | |
} | |
out << message << endl; | |
if (should_save_to_file_) { | |
if(!module.empty()) { | |
file_ << module << ": "; | |
} | |
file_ << message << endl; | |
} | |
} | |
void LoggerChannel::log(ofLogLevel level, | |
const string & module, | |
const char* format, ...) { | |
va_list args; | |
va_start(args, format); | |
log(level, module, format, args); | |
va_end(args); | |
} | |
void LoggerChannel::log(ofLogLevel level, | |
const string & module, | |
const char* format, | |
va_list args) { | |
FILE * out = level < OF_LOG_ERROR ? stdout : stderr; | |
fprintf(out, "%s [%s] ", ofGetTimestampString().c_str(), ofGetLogLevelName(level, true).c_str()); | |
if(!module.empty()) { | |
fprintf(out, "%s: ", module.c_str()); | |
} | |
vfprintf(out, format, args); | |
fprintf(out, "\n"); | |
if (should_save_to_file_) { | |
file_ << "[" << ofGetLogLevelName(level, true) << "] "; | |
if(!module.empty()) { | |
file_ << module << ": "; | |
} | |
file_ << ofVAArgsToString(format, args) << endl; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "ofLog.h" | |
namespace aoi { | |
void PushLogLevel(); | |
void PopLogLevel(); | |
#pragma mark - | |
class ScopedLogLevel { | |
private: | |
ofLogLevel level; | |
public: | |
ScopedLogLevel() { level = ofGetLogLevel(); } | |
~ScopedLogLevel() { ofSetLogLevel(level); } | |
}; | |
#pragma mark - | |
class LoggerChannel : public ofBaseLoggerChannel { | |
private: | |
ofFile file_; | |
std::string file_path_; | |
bool should_save_to_file_; | |
public: | |
LoggerChannel(); | |
~LoggerChannel(); | |
void setup(std::string path, bool append = false); | |
void log(ofLogLevel level, const string & module, const string & message); | |
void log(ofLogLevel level, const string & module, const char* format, ...); | |
void log(ofLogLevel level, const string & module, const char* format, va_list args); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofMain.h" | |
#include "log.h" | |
class ofApp : public ofBaseApp { | |
public: | |
ofApp() | |
{} | |
void setup() { | |
ofSetFrameRate(1); | |
ofSetVerticalSync(true); | |
auto lc = std::make_shared<aoi::LoggerChannel>(); | |
lc->setup("ore_log.txt", true); | |
ofSetLoggerChannel(lc); | |
//! | |
ofSetLogLevel(OF_LOG_VERBOSE); | |
ofLogVerbose("1") << "message here"; | |
ofLogNotice("2") << "message here"; | |
ofLogWarning("3") << "message here"; | |
ofLogError("4") << "message here"; | |
ofLogFatalError("5") << "message here"; | |
//! | |
aoi::PushLogLevel(); { | |
ofSetLogLevel(OF_LOG_SILENT); | |
ofLogWarning("silent") << "push test, this shouldn't be on console..."; | |
} aoi::PopLogLevel(); | |
ofLogWarning("not silent") << "push test, this should be on console!"; | |
//! | |
{ | |
aoi::ScopedLogLevel scope; | |
ofSetLogLevel(OF_LOG_SILENT); | |
ofLogWarning("silent") << "scoped log test, this shouldn't be on console..."; | |
} | |
ofLogWarning("not silent") << "scoped log test, this shouldn't be on console!"; | |
} | |
void update() { | |
ofLogNotice("ofApp::update") << "tick"; | |
} | |
}; | |
#pragma mark - | |
#pragma mark main | |
int main(){ | |
ofSetupOpenGL(500, 500, OF_WINDOW); | |
ofRunApp(new ofApp()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment