Solution46
LibLogOut.h
[詳解]
1 //=============================================================================
2 /// @file
3 /// ログ出力ライブラリヘッダファイル
4 ///
5 /// ログ出力ライブラリヘッダファイルです。
6 ///
7 /// $Id: LibLogOut.h 245 2019-03-20 15:03:41Z admin $
8 /// $Date: 2019-03-21 00:03:41 +0900 (2019/03/21 (木)) $
9 /// $Author: admin $
10 ///
11 /// @attention なし
12 
13 #pragma once
14 #pragma managed( push, off )
15 
16 //=============================================================================
17 // インクルードファイル
18 #include <LibLogOutDef.h>
19 #include <CMapping.h>
20 #include <CStreamOut.h>
21 #include <CEvent.h>
22 #include <CFileStream.h>
23 #include <CThread.h>
24 
25 //=============================================================================
26 // マクロ定義
27 #define LogOutDebug LibLogOut::CLibLogOut::EOutputDevice::Debug ///< デバッグ出力デバイスマクロ
28 #define LogOutConsole LibLogOut::CLibLogOut::EOutputDevice::Console ///< コンソール出力デバイスマクロ
29 #define LogOutFile LibLogOut::CLibLogOut::EOutputDevice::File ///< ファイル出力デバイスマクロ
30 #define LogOutDeviceMax LibLogOut::CLibLogOut::DEVICE_MAX ///< 出力デバイス最大値マクロ
31 #define LogOutInstance() LibLogOut::CLibLogOut::GetInstance() ///< インスタンス取得マクロ
32 #define LogOutChar(...) LogOutInstance().OutputChar(__VA_ARGS__) ///< 1文字出力マクロ
33 #define LogOutString(...) LogOutInstance().OutputString(__VA_ARGS__) ///< 文字列出力マクロ
34 #define LogOutFormat(...) LogOutInstance().OutputFormat(__VA_ARGS__) ///< 書式設定文字列出力マクロ
35 #define LogOutNewLine() LogOutInstance().OutputNewLine() ///< 改行出力マクロ
36 #define LogOutLineString(...) LogOutInstance().OutputLineString(__VA_ARGS__) ///< 文字列行出力マクロ
37 #define LogOutLineFormat(...) LogOutInstance().OutputLineFormat(__VA_ARGS__) ///< 書式設定文字列行出力マクロ
38 #define LogOutExLineFormat(...) LogOutInstance().ExOutputLineFormat(__VA_ARGS__) ///< 拡張書式設定文字列行出力マクロ
39 #define LogOutNoIndent(...) LogOutInstance().ExOutputLineFormat(true,false,__VA_ARGS__) ///< インデントなし書式設定文字列行出力マクロ
40 #define LogOutLine(...) LogOutInstance().ExOutputLineFormat(false,false,__VA_ARGS__) ///< ヘッダインデントなし書式設定文字列行出力マクロ
41 #define LogOutExeFile() LogOutInstance().GetExeFile() ///< 実行ファイル名取得マクロ
42 #define LogOutFunction() LogOutLineFormat(L"%s!%s()",LogOutExeFile(),__FUNCTIONW__) ///< 関数マクロ
43 #define LogOutIndent() LogOutInstance().Indent() ///< インデントマクロ
44 #define LogOutUnindent() LogOutInstance().Unindent() ///< アンインデントマクロ
45 #define LogOutBlockIn(...) LogOutLineFormat(__VA_ARGS__);LogOutIndent() ///< ブロック開始マクロ
46 #define LogOutBlockOut(...) LogOutUnindent();LogOutLineFormat(__VA_ARGS__) ///< ブロック終了マクロ
47 #define LogOutHeader() LogOutBlockIn (L"%s!%s() {", LogOutExeFile(),__FUNCTIONW__) ///< 関数ヘッダマクロ
48 #define LogOutRefferCount() LogOutInstance().GetRefferCount() ///< 参照プロセスカウント取得マクロ
49 #define LogOutBufferCount() LogOutInstance().GetCount() ///< バッファカウント取得マクロ
50 #define LogOutFooter() LogOutBlockOut(L"} // %s!%s()",LogOutExeFile(),__FUNCTIONW__) ///< 関数フッタマクロ
51 #define LogOutFlush() LogOutInstance().Flush() ///< フラッシュマクロ
52 #define LogOutDevice(...) LogOutInstance().SetDeviceProcess(__VA_ARGS__) ///< 出力デバイスプロセス設定マクロ
53 
54 //=============================================================================
55 // ログ出力ライブラリ名前空間
56 namespace LibLogOut {
57  //=========================================================================
58  /// ログ出力ライブラリクラス
59  ///
60  /// ログ出力ライブラリクラスです。
61  ///
62  /// @attention シングルトンクラスです。
63  ///
64  class LIB_LOGOUT_API CLibLogOut final: public CMapping, public CStreamOut {
65  //=====================================================================
66  // 公開型定義
67  public:
68  //---------------------------------------------------------------------
69  /// 出力デバイス種別列挙体
70  ///
71  /// 出力デバイス種別列挙体です。
72  ///
73  /// @attention なし
74  ///
75  enum class EOutputDevice {
76  Debug = 0, ///< デバッグ出力
77  Console, ///< コンソール出力
78  File, ///< ファイル出力
79  Size ///< 列挙体サイズ
80  };
81 
82  //=====================================================================
83  // 静的公開定数
84  public:
85  static int const DEVICE_MAX = static_cast< int >( EOutputDevice::Size ); ///< 出力デバイス最大値
86 
87  //=====================================================================
88  // 構築子と解体子
89  protected:
90  //---------------------------------------------------------------------
91  /// コンストラクタ
92  ///
93  /// コンストラクタです。
94  ///
95  /// @param なし
96  /// @return なし
97  /// @attention なし
98  ///
99  explicit CLibLogOut() noexcept;
100 
101  //---------------------------------------------------------------------
102  /// デストラクタ
103  ///
104  /// デストラクタです。
105  ///
106  /// @param なし
107  /// @return なし
108  /// @attention なし
109  ///
110  virtual ~CLibLogOut() noexcept;
111 
112  //=====================================================================
113  // インライン公開関数
114  public:
115  //---------------------------------------------------------------------
116  /// 実行ファイル名取得関数
117  ///
118  /// 実行ファイル名取得関数です。
119  ///
120  /// @param なし
121  /// @return 文字列ポインタ
122  /// @attention なし
123  ///
124  inline virtual wchar_t const* GetExeFile() noexcept { return m_szExeFile; }
125 
126  //=====================================================================
127  // 公開関数
128  public:
129  //---------------------------------------------------------------------
130  /// 文字列出力関数
131  ///
132  /// 文字列出力関数です。
133  ///
134  /// @param[in] pszString 文字列ポインタ
135  /// @return 実行結果
136  /// - true 成功
137  /// - false 失敗
138  /// @attention オーバーライド関数です。
139  ///
140  virtual bool OutputString( wchar_t const* pszString ) noexcept override;
141 
142  //---------------------------------------------------------------------
143  /// 行番号更新関数
144  ///
145  /// 行番号更新関数です。
146  ///
147  /// @param なし
148  /// @return 実行結果
149  /// - true 成功
150  /// - false 失敗
151  /// @attention オーバーライド関数です。
152  ///
153  virtual bool UpdateLineNumber() noexcept override;
154 
155  //---------------------------------------------------------------------
156  /// 行番号取得関数
157  ///
158  /// 行番号取得関数です。
159  ///
160  /// @param なし
161  /// @return 行番号
162  /// @attention オーバーライド関数です。
163  ///
164  virtual int GetLineNumber() noexcept override;
165 
166  //---------------------------------------------------------------------
167  /// インデントカウント取得関数
168  ///
169  /// インデントカウント取得関数です。
170  ///
171  /// @param なし
172  /// @return インデントカウント
173  /// @attention オーバライド関数です。
174  ///
175  virtual int GetIndentCount() noexcept override;
176 
177  //---------------------------------------------------------------------
178  /// インデント関数
179  ///
180  /// インデント関数です。
181  ///
182  /// @param なし
183  /// @return 実行結果
184  /// - true 成功
185  /// - false 失敗
186  /// @attention オーバライド関数です。
187  ///
188  virtual bool Indent() noexcept override;
189 
190  //---------------------------------------------------------------------
191  /// アンインデント関数
192  ///
193  /// アンインデント関数です。
194  ///
195  /// @param なし
196  /// @return 実行結果
197  /// - true 成功
198  /// - false 失敗
199  /// @attention オーバライド関数です。
200  ///
201  virtual bool Unindent() noexcept override;
202 
203  //---------------------------------------------------------------------
204  /// 参照プロセスカウント取得関数
205  ///
206  /// 参照プロセスカウント取得関数です。
207  ///
208  /// @param なし
209  /// @return バッファカウント
210  /// @attention なし
211  ///
212  virtual int GetRefferCount() noexcept;
213 
214  //---------------------------------------------------------------------
215  /// バッファカウント取得関数
216  ///
217  /// バッファカウント取得関数です。
218  ///
219  /// @param なし
220  /// @return バッファカウント
221  /// @attention なし
222  ///
223  virtual int GetCount() noexcept;
224 
225  //---------------------------------------------------------------------
226  /// フラッシュ関数
227  ///
228  /// フラッシュ関数です。
229  ///
230  /// @param なし
231  /// @return 実行結果
232  /// - true 成功
233  /// - false 失敗
234  /// @attention なし
235  ///
236  virtual bool Flush() noexcept;
237 
238  //---------------------------------------------------------------------
239  /// 出力デバイスプロセス設定関数
240  ///
241  /// 出力デバイスプロセス設定関数です。
242  ///
243  /// @param[in] eDevice デバイス種別
244  /// @param[in] dwProcessId プロセスID
245  /// @return 実行結果
246  /// - true 成功
247  /// - false 失敗
248  /// @attention なし
249  ///
250  virtual bool SetDeviceProcess( EOutputDevice eDevice, DWORD dwProcessId ) noexcept;
251 
252  //=====================================================================
253  // 限定公開関数
254  protected:
255  //---------------------------------------------------------------------
256  /// プロセス情報更新関数
257  ///
258  /// プロセス情報更新関数です。
259  ///
260  /// @param[in] bCurrent カレントプロセス削除フラグ
261  /// @return 実行結果
262  /// - true 成功
263  /// - false 失敗
264  /// @attention なし
265  ///
266  virtual bool UpdateProcessInfo( bool bCurrent = false ) noexcept;
267 
268  //---------------------------------------------------------------------
269  /// デバイス出力関数
270  ///
271  /// デバイス出力関数です。
272  ///
273  /// @param なし
274  /// @return 実行結果
275  /// - true 成功
276  /// - false 失敗
277  /// @attention なし
278  ///
279  virtual bool OutputDevice() noexcept;
280 
281  //=====================================================================
282  // インライン静的公開関数
283  public:
284  //---------------------------------------------------------------------
285  /// インスタンス取得関数
286  ///
287  /// インスタンス取得関数です。
288  ///
289  /// @param なし
290  /// @return インスタンス参照
291  /// @attention なし
292  ///
293  inline static CLibLogOut& GetInstance() noexcept { return s_cInstance; }
294 
295  //=====================================================================
296  // 静的公開関数
297  public:
298  //---------------------------------------------------------------------
299  /// プロジェクト種別取得関数
300  ///
301  /// プロジェクト種別取得関数です。
302  ///
303  /// @param なし
304  /// @return 文字列ポインタ
305  /// @attention なし
306  ///
307  static wchar_t const* GetProjectType() noexcept;
308 
309  //---------------------------------------------------------------------
310  /// インスタンスハンドル取得関数
311  ///
312  /// インスタンスハンドル取得関数です。
313  ///
314  /// @param なし
315  /// @return インスタンスハンドル
316  /// @attention なし
317  ///
318  static HINSTANCE GetInstanceHandle() noexcept;
319 
320  //=====================================================================
321  // 静的限定公開関数
322  protected:
323  //---------------------------------------------------------------------
324  /// サブスレッド関数
325  ///
326  /// サブスレッド関数です。
327  ///
328  /// @param[in] lpParam 引数ポインタ
329  /// @return 終了コード
330  /// @attention なし
331  ///
332  static DWORD WINAPI SubThreadProc( LPVOID lpParam ) noexcept;
333 
334  //---------------------------------------------------------------------
335  /// 新規ログ出力ファイルパス取得関数
336  ///
337  /// 新規ログ出力ファイルパス取得関数です。
338  ///
339  /// @param[out] pszPath 文字列バッファポインタ
340  /// @param[in] nSize バッファサイズ
341  /// @return 実行結果
342  /// - true 成功
343  /// - false 失敗
344  /// @attention なし
345  ///
346  static bool GetNewLogFilePath( wchar_t* pszPath, int nSize ) noexcept;
347 
348  //=====================================================================
349  // 静的限定公開定数
350  protected:
351  static int const BUFFER_SIZE = ( 1024 * 1024 - 1 ); ///< 文字列バッファサイズ
352  static int const PROCESS_MAX = 256; ///< 最大参照プロセス数
353  static DWORD const NOTIFY_WAIT = 3000; ///< 通知イベント待機時間
354 
355  //=====================================================================
356  // 限定公開型定義
357  protected:
358  //---------------------------------------------------------------------
359  /// 参照プロセス情報構造体
360  ///
361  /// 参照プロセス情報構造体です。
362  ///
363  /// @attention なし
364  ///
365  struct SProcessInfo {
366  DWORD m_dwProcessId; ///< プロセスID
367  int m_nIndent; ///< インデントカウント
368  };
369 
370  //---------------------------------------------------------------------
371  /// 共有メモリ情報構造体
372  ///
373  /// 共有メモリ情報構造体です。
374  ///
375  /// @attention なし
376  ///
377  struct SMappingInfo {
378  int m_nReffer; ///< 参照プロセスカウント
379  wchar_t m_szBuffer[ BUFFER_SIZE + 1 ]; ///< 文字列バッファ
380  int m_nCount; ///< バッファカウント
381  int m_nIndent; ///< インデントカウント
382  int m_nLine; ///< 行番号
383  wchar_t m_szPath[ MAX_PATH ]; ///< ログ出力ファイルパス
384  bool m_bEnable[ DEVICE_MAX ]; ///< 出力許可フラグ配列
385  DWORD m_dwProcessId[ DEVICE_MAX ]; ///< 出力プロセスID配列
386  SProcessInfo m_sProcess[ PROCESS_MAX ]; ///< 参照プロセス情報構造体配列
387  EOutputDevice m_eDevice; ///< 出力要求デバイス種別
388  int m_nRequestPos; ///< 出力要求開始位置
389  int m_nRequestSize; ///< 出力要求サイズ
390  };
391 
392  //=====================================================================
393  // 限定公開変数
394  protected:
395  SMappingInfo* m_psMappingInfo; ///< 共有メモリ情報構造体ポインタ
396  CEvent m_cEventRequest; ///< 要求イベント
397  CEvent m_cEventNotify; ///< 通知イベント
398  CFileStream m_cFileLog; ///< ログ出力ファイル
399  CThread m_cThreadSub; ///< サブスレッド
400  wchar_t m_szExeFile[ MAX_PATH ]; ///< 実行ファイル名
401 
402  //=====================================================================
403  // 静的限定公開文字列定数
404  protected:
405  static wchar_t const* const OBJECT_NAME; ///< オブジェクト名
406  static wchar_t const* const EVENT_REQUEST_FORMAT; ///< 要求イベント名書式設定文字列
407  static wchar_t const* const EVENT_NOTIFY_NAME; ///< 通知イベント名
408 
409  //=====================================================================
410  // 静的限定公開変数
411  protected:
412  static CLibLogOut s_cInstance; ///< 静的インスタンス
413 
414  //=====================================================================
415  // 削除関数
416  private:
417  CLibLogOut( CLibLogOut const& ) = delete;
418  CLibLogOut& operator=( CLibLogOut const& ) = delete;
419  };
420 }
421 
422 #pragma managed( pop )
CThread m_cThreadSub
サブスレッド
Definition: LibLogOut.h:399
共有メモリ情報構造体
Definition: LibLogOut.h:377
static wchar_t const *const EVENT_NOTIFY_NAME
通知イベント名
Definition: LibLogOut.h:407
スレッドクラスヘッダファイル
ログ出力ライブラリクラス
Definition: LibLogOut.h:64
int m_nRequestPos
出力要求開始位置
Definition: LibLogOut.h:388
CEvent m_cEventRequest
要求イベント
Definition: LibLogOut.h:396
static CLibLogOut s_cInstance
静的インスタンス
Definition: LibLogOut.h:412
ログ出力ライブラリ名前空間
Definition: LibLogOut.h:56
ストリーム出力クラスヘッダファイル
参照プロセス情報構造体
Definition: LibLogOut.h:365
ファイルストリームクラス
Definition: CFileStream.h:31
SMappingInfo * m_psMappingInfo
共有メモリ情報構造体ポインタ
Definition: LibLogOut.h:395
EOutputDevice
出力デバイス種別列挙体
Definition: LibLogOut.h:75
イベントクラス
Definition: CEvent.h:30
EOutputDevice m_eDevice
出力要求デバイス種別
Definition: LibLogOut.h:387
マッピングクラスヘッダファイル
static wchar_t const *const OBJECT_NAME
オブジェクト名
Definition: LibLogOut.h:405
int m_nIndent
インデントカウント
Definition: LibLogOut.h:367
int m_nIndent
インデントカウント
Definition: LibLogOut.h:381
static wchar_t const *const EVENT_REQUEST_FORMAT
要求イベント名書式設定文字列
Definition: LibLogOut.h:406
ストリーム出力クラス
Definition: CStreamOut.h:30
#define LIB_LOGOUT_API
ダイナミックライブラリインポート宣言
Definition: LibLogOutDef.h:27
int m_nReffer
参照プロセスカウント
Definition: LibLogOut.h:378
int m_nRequestSize
出力要求サイズ
Definition: LibLogOut.h:389
ファイルストリームクラスヘッダファイル
スレッドクラス
Definition: CThread.h:30
マッピングクラス
Definition: CMapping.h:30
int m_nCount
バッファカウント
Definition: LibLogOut.h:380
static CLibLogOut & GetInstance() noexcept
インスタンス取得関数
Definition: LibLogOut.h:293
イベントクラスヘッダファイル
ログ出力ライブラリ定義ヘッダファイル
CFileStream m_cFileLog
ログ出力ファイル
Definition: LibLogOut.h:398
CEvent m_cEventNotify
通知イベント
Definition: LibLogOut.h:397
DWORD m_dwProcessId
プロセスID
Definition: LibLogOut.h:366