Last active
February 14, 2017 09:09
-
-
Save usagi/95cb1309c9d5d5971ee1a0549360dfa6 to your computer and use it in GitHub Desktop.
C++のちょっとしたアプリでさっと使いやすい簡易ロガーの実装例 ref: http://qiita.com/usagi/items/d4aec8d3f748f4ba9d6a
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
小さなサンプルアプリ作る | |
↓ | |
でもまあロガーくらい入れて作りたい | |
↓ | |
boost::log入れるのめんどくさいしちょっとしたやつ | |
↓ | |
気づけばいつものロガーコードを書いている |
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
//↓これを定義すると 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