Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@usagi
Last active February 14, 2017 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usagi/95cb1309c9d5d5971ee1a0549360dfa6 to your computer and use it in GitHub Desktop.
Save usagi/95cb1309c9d5d5971ee1a0549360dfa6 to your computer and use it in GitHub Desktop.
C++のちょっとしたアプリでさっと使いやすい簡易ロガーの実装例 ref: http://qiita.com/usagi/items/d4aec8d3f748f4ba9d6a
小さなサンプルアプリ作る
でもまあロガーくらい入れて作りたい
boost::log入れるのめんどくさいしちょっとしたやつ
気づけばいつものロガーコードを書いている
//↓これを定義すると easy_logger は LOGI などで何も出力しなくなります。
//#define DISABLE_USAGI_LOG_EASY_LOGGER
#include <usagi/log/easy_logger.hxx>
#include <thread>
#include <random>
auto main() -> int
{
// 単純な使い方
LOGI << "test1";
// 複数の operator<< で繋いでも綺麗に出力されます。
LOGD << "test2 multiple output operators in the one line " << 12345 << ' ' << 1.2345f;
// マルチスレッドで競争的にログ出力しても分断されずに綺麗に出力されます。
std::random_device r;
auto a = std::thread( [&]{ for ( auto n = 0; n < 16; ++n ) LOGW << "/(^o^)\ " << r() << ' ' << r()<< ' ' << r() << ' ' << ' ' << r(); } );
std::thread( [&]{ for ( auto n = 0; n < 16; ++n ) LOGE << "\(^o^)/ " << r() << ' ' << r()<< ' ' << r() << ' ' << ' ' << r(); } ).join();
a.join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment