glogのダウンロード、インストール、使用
Glog Download Install
- 最近、私はプログラムにロギングシステムを追加することを学んでいました。グーグルのオープンソースが提供する軽量のログライブラリであるglogを検索資料で見つけたので、ピットに入るプロセスを開始しました。
サンザシのダウンロード
最初にやらなければならないことは、glogのソースコードをダウンロードすることです。
1)直接クリックする github リンク 「Github / glogソースコード」 gibhubページに入る
2)次にクリックします クローンまたはダウンロード--------> Zipをダウンロード
Githubソースのダウンロード
3)glog-masterを解凍します。ディレクトリ構造は次のとおりです。 CMakeLists.txt 。次回は cmake このツール
glog-masterディレクトリ構造
4)cmakeへの公式リンク 「Cmakeダウンロードアドレス」 ダウンロード cmake 。私が選んだ
cmakeダウンロード
5)cmakeインストールパッケージを解凍した後、私たちがしなければならないのは置くだけです 環境変数を追加し、コンピューターを右クリック->高度なシステム設定->環境変数。
6)を押します Win + R そして、 cmake-gui 、次のウィンドウが表示されます
7)[構成]ボタンをクリックして、VisualStudioバージョン、いくつかの赤いオプションがポップアップ表示されます。簡単な変更を加えるだけです。
パスをインストールすることを忘れないでください。システムディレクトリを選択しないでください。管理者権限がないため、続行操作は失敗します。
8)クリック 生成、プロジェクトを開く ソリューションを開くには
9)ちょうど CMAKE_INSTALL_PREFIX 生成されたglog静的ライブラリファイルをパスで確認できます
いくつかのglog静的ライブラリが生成されたら、次のステップはglogを使用する簡単な例です。
上司、さあ
例から始めて、VSを使用してコンソールアプリケーションを直接作成してから、次の構成を行います。
- プロジェクトのプロパティを右クリックします
- 構成プロパティ-> C / C ++->一般->追加のインクルードディレクトリ->追加/ include
- 構成プロパティ->リンカ->一般->追加のライブラリディレクトリ->追加/ lib
- 構成プロパティ->リンカ->入力->追加の依存関係->追加glogd.lib(プログラムがリリースバージョンの場合はglog.libを記述します)
次に、glogコードの簡単な例を書くことができます
// USEglog.cpp: defines the entry point of the console application. // #include 'stdafx.h' #define GOOGLE_GLOG_DLL_DECL // Because we use the glog static library, we must define this macro #include 'glog/logging.h' int main(int argc, char* argv[]) { google::InitGoogleLogging(argv[0]) // Globally initialize glog google::SetStderrLogging(google::INFO) // Set the output level of glog, the meaning here is to output information above INFO level LOG(INFO) << 'This is my first glog INFO' // Just use it like a C++ standard stream, then define this message as INFO level google::ShutdownGoogleLogging() // Close glog globally return 0 }
出力は次のとおりです
私たちがしていることとcoutの違いは何ですか<< xxxx? Yes, there is no difference, I just pit everyone! ! ! ! Just kidding, see the next configuration
glogのいくつかの設定を変更します
1)サンプルプログラムのコードを変更します
// USEglog.cpp: defines the entry point of the console application. // #include 'stdafx.h' #define GOOGLE_GLOG_DLL_DECL // Because we use the glog static library, we must define this macro #include 'glog/logging.h' int main(int argc, char* argv[]) { google::InitGoogleLogging(argv[0]) // Globally initialize glog google::SetStderrLogging(google::WARNING) // Set the output level of glog, the meaning here is to output information above WARNING level LOG(INFO) << 'This is my first glog INFO' // Just use it like a C++ standard stream, define this message as INFO level LOG(WARNING) << 'This is my first glog WARNING' // Just use it like a C++ standard stream, define this message as WARNING level LOG(ERROR) << 'This is my first glog ERROR' // Just use it like C++ standard stream, define this message as ERROR level google::ShutdownGoogleLogging() // Close glog globally return 0 }
2)出力は次のとおりです。警告およびエラーレベルの情報を確認できます。
3)しかし、INFOレベルの情報は表示されませんでした。これは、コードにそのような行があるためです。 A
google::SetStderrLogging(google::WARNING) // Set the output level of glog, the meaning here is to output information above WARNING level
glogで定義できる情報には4つのレベルがあります
- 情報
- 警告
- エラー
- 致命的(このレベルでの出力は、プログラムの強制的な中断を引き起こします)
4)端末の色を区別するためのコード行を追加します
FLAGS_colorlogtostderr = true // Turn on terminal color differentiation
glogログ情報をディスクログファイルに書き込む
1)サンプルプログラムコードを次のように変更します
// USEglog.cpp: defines the entry point of the console application. // #include 'stdafx.h' #define GOOGLE_GLOG_DLL_DECL // Because we use the glog static library, we must define this macro #include 'glog/logging.h' int main(int argc, char* argv[]) { google::InitGoogleLogging(argv[0]) // Globally initialize glog google::SetStderrLogging(google::GLOG_INFO) // Set the output level of glog, the meaning here is to output information above INFO level // Set the path and prefix name of the information log file above the INFO level google::SetLogDestination(google::GLOG_INFO, 'C:\users\xxxx\documents\logdir\INFO_') // Set the path and prefix name of the information log file above WARNING level google::SetLogDestination(google::GLOG_WARNING, 'C:\users\xxxx\documents\logdir\WARNING_') // Set the path and prefix name of the information log file above the ERROR level google::SetLogDestination(google::GLOG_ERROR, 'C:\users\xxxx\documents\logdir\ERROR_') FLAGS_colorlogtostderr = true // Turn on terminal color differentiation LOG(INFO) << 'This is my first glog INFO' // Just use it like a C++ standard stream, define this message as INFO level LOG(WARNING) << 'This is my first glog WARNING' // Just use it like a C++ standard stream, define this message as WARNING level LOG(ERROR) << 'This is my first glog ERROR' // Just use it like C++ standard stream, define this message as ERROR level google::ShutdownGoogleLogging() // Close glog globally return 0 }
2)設定したディレクトリにログファイルが表示されます
- INFO_:ログ情報をINFOおよびINFOレベルより上に格納します
- WARNING_:WARNING以上のWARNINGレベルのログ情報を保存します
- ERROR_:ERRORおよびERRORレベルを超えるログ情報を格納します
ファンワイ
プログラムにwindows.hヘッダーファイルが含まれている場合は、追加の作業を行う必要があります。
に#include先立って、#define GLOG_NO_ABBREVIATED_SEVERITIES
#define GLOG_NO_ABBREVIATED_SEVERITIES #include 'glog/logging.h'
glog静的ライブラリを使用している場合
しなければならない#define GOOGLE_GLOG_DLL_DECL
#define GOOGLE_GLOG_DLL_DECL #define GLOG_NO_ABBREVIATED_SEVERITIES #include 'glog/logging.h'