CTR-Pia  5.4.3
Game Communication Engine
 全て クラス ネームスペース 関数 変数 型定義 列挙型 列挙型の値 ページ
common_Report.h
1 /*--------------------------------------------------------------------------------*
2  Copyright (C)Nintendo All rights reserved.
3 
4  These coded instructions, statements, and computer programs contain proprietary
5  information of Nintendo and/or its licensed developers and are protected by
6  national and international copyright laws. They may not be disclosed to third
7  parties or copied or duplicated in any form, in whole or in part, without the
8  prior written consent of Nintendo.
9 
10  The content herein is highly confidential and should be handled accordingly.
11  *--------------------------------------------------------------------------------*/
12 
13 
14 #pragma once
15 
16 #include <nn/pia/common/common_RootObject.h>
17 #include <nn/pia/common/common_Time.h>
18 
19 namespace nn
20 {
21 namespace pia
22 {
23 namespace common
24 {
25 
26 
27 /*!
28  @brief アプリ開発者に伝えるレポートをプリントするクラスです。
29 
30  @details レポートにはレベルを設定することができ、レベル毎にログの
31  表示/非表示を切り替えることができます。
32 
33  */
34 class Report : public RootObject
35 {
36 public:
37  // ログレベルのビットマスク。
38  static const uint32_t LevelInfo = 0x00000001 << 0; //!< Pia 内部の詳細情報レベル。
39  static const uint32_t LevelWarning = 0x00000001 << 1; //!< 警告レベル(修正すべきだが無視可能)。
40  static const uint32_t LevelError = 0x00000001 << 2; //!< エラーレベル(復帰可能)。
41 
42  //n1589:IsFlagSetチェックのコードを最短化
43  /*!
44  @cond PRIVATE
45  */
46  static bool IsFlagSetStatic(uint64_t flag)
47  {
48 #if NN_PIA_RELEASE
49  NN_PIA_UNUSED_IN_RELEASE(flag);
50  return false;
51 #else
52  if (GetGlobalObject() == NULL)
53  {
54  return false;
55  }
56 
57  return GetGlobalObject()->IsFlagSet(flag);
58 #endif
59  }
60  //! @endcond
61 
62  /*!
63  @brief コンストラクタです。ログレベルのビットマスクは全てのビットが1の状態でオブジェクトが構築されます。
64  */
65  Report(void);
66 
67  /*!
68  @brief コンストラクタです。引数でログレベルのマスクを指定できます。
69 
70  @param[in] mask ログレベルのビットマスク。
71  */
72  explicit Report(uint32_t mask);
73 
74  /*!
75  @brief デストラクタです。
76  */
77  ~Report();
78 
79  /*!
80  @brief マスクを設定します。特定のレベルのログ表示を抑制したりすることができます。
81 
82  @details この API により、出力されるログを絞り込むことができます。
83 
84  @param[in] mask ログレベルのビットマスクの論理和。
85  */
86  void SetMask(uint32_t mask)
87  {
88  m_mask = mask;
89  }
90 
91  /*!
92  @brief 特定のフラグが設定されているかどうかを返します。
93 
94  @details なお、 IsFlasSet ( TraceFlagNever ) の結果は true となります。
95  <br>
96  @return 特定のフラグが設定されていれば true 、そうでなければ false を返します。
97 
98  @param[in] flag 問い合わせるトレースフラグ。
99  */
100  bool IsFlagSet(uint64_t flag) const
101  {
102 #if NN_PIA_RELEASE
103  NN_PIA_UNUSED_IN_RELEASE(flag);
104  return false;
105 #else
106  return (m_mask & flag) == flag;
107 #endif
108  }
109 
110  /*!
111  @cond PRIVATE
112  @brief コンソールに文字列を書き出します。可変引数に対応しています。
113 
114  @param[in] mask ログレベルのビットマスク。
115  @param[in] pStr C標準ライブラリの printf 引数と同様の書式を与えます。
116  */
117  void Write(uint32_t mask, const char* pStr, ...);
118  //! @endcond
119 
120 
121  /*!
122  @cond PRIVATE
123  @brief コンソールにキャプション文字列を書き出します。
124 
125  @param[in] mask ログレベルのビットマスク。
126  */
127  void WriteCaption(uint32_t mask);
128  //! @endcond
129 
130 
131  /*!
132  @brief 時刻表示の ON / OFF を切り替えます。
133  @details ON の時は "[Pia:12345] ..." のように基準時刻からの経過時間をミリ秒単位で表示します。
134  @param[in] isPrint true なら時刻を表示します。
135  */
136  void SetPrintTime(bool isPrint)
137  {
138  m_IsPrintTime = isPrint;
139  }
140 
141 
142  /*!
143  @brief 時刻表示の ON / OFF を取得します。
144  @return 時刻表示が ON になっていれば true を返します。
145  */
146  bool IsPrintTime() const
147  {
148  return m_IsPrintTime;
149  }
150 
151 
152  /*!
153  @brief 時刻表示の基準時刻を現在の時刻にします。
154  */
156  {
157  m_BaseTime.SetNow();
158  }
159 
160 
161  /*!
162  @brief グローバルログオブジェクトを登録します。
163 
164  @details 引数で指定した Report オブジェクトが、グローバルログオブジェクトとして登録されます。
165  PIA_REPORT() マクロは、ここで登録された Report オブジェクトを利用してログ出力を行います。
166 
167  @param[in] pReport 登録するReportオブジェクトへのポインタ。
168  */
169  static void SetGlobalObject(Report* pReport)
170  {
171  s_pReport = pReport;
172  }
173 
174  /*!
175  @brief SetGlobalObject() で指定された Report オブジェクトへのポインタが返されます。
176 
177  @details nn::pia::common::Initialize() を呼ぶと、内部で暗黙的に Report インスタンスが作成され、
178  そのインスタンスがグローバルログオブジェクトとしてセットされます。
179 
180  @return グローバルログオブジェクトとして登録されているインスタンスへのポインタ。
181  */
182  static Report* GetGlobalObject(void)
183  {
184  return s_pReport;
185  }
186 
187 
188 private:
189  // グローバルオブジェクトを指すポインタ。
190  static Report* s_pReport;
191 
192  // マスク設定は Report インスタンス毎に保持します。
193  uint32_t m_mask;
194 
195  Time m_BaseTime;
196  bool m_IsPrintTime;
197 
198  NN_PIA_DISALLOW_COPY(Report);
199 };
200 
201 
202 /*!
203  @brief 簡単にログを出力するために用意したマクロです。
204 
205  @details このマクロでは、グローバルとしてセットした Report オブジェクトが利用されます。
206  また、自動的に接頭辞と改行が付加されます。
207  @details リリースビルドにおいては、このマクロは何もしません。
208 
209  @param[in] mask ログのビットマスク。
210  */
211 #if NN_PIA_UNDER_DEVELOP
212 #define PIA_REPORT(mask, ...) \
213  \
214 { \
215  ::nn::pia::common::Report* pPiaReport = ::nn::pia::common::Report::GetGlobalObject(); \
216  if (pPiaReport) \
217  { \
218  pPiaReport->WriteCaption(mask); \
219  pPiaReport->Write(mask, __VA_ARGS__); \
220  pPiaReport->Write(mask, "\n"); \
221  } \
222  \
223 }
224 #else
225 #define PIA_REPORT(mask, ...) \
226  { \
227  }
228 #endif
229 
230 /*!
231  @brief ファイル名と関数名を含めたログ出力を行うマクロです。
232 
233  @details このマクロでは、グローバルとしてセットしたReportオブジェクトが利用されます。
234  また、自動的に接頭辞と改行が付加されます。
235  @details リリースビルドにおいては、このマクロは何もしません。
236 
237  @param[in] mask ログのビットマスク。
238  */
239 #if NN_PIA_UNDER_DEVELOP
240 #define PIA_REPORT_EX(mask, ...) \
241  \
242 { \
243  nn::pia::common::Report* pPiaReport = nn::pia::common::Report::GetGlobalObject(); \
244  if (pPiaReport) \
245  { \
246  pPiaReport->WriteCaption(mask); \
247  pPiaReport->Write(mask, "%s %s:", PIA_CODE_POSITION_FILE, PIA_CODE_POSITION_FUNC); \
248  pPiaReport->Write(mask, __VA_ARGS__); \
249  pPiaReport->Write(mask, "\n"); \
250  } \
251  \
252 }
253 #else
254 #define PIA_REPORT_EX(mask, ...) \
255  { \
256  }
257 #endif
258 
259 // ログ出力と Result の return を統合したマクロです。
260 // ログのレベルは常に LEVEL_ERROR なので、このマクロに渡す Result は
261 // プログラミングエラー相当のものに限定します。
262 // common モジュールの初期化前は PIA_BASIC_PRINT されます。
263 // リリースビルドにおいては、単に Result を return するだけとなります。
264 #if NN_PIA_UNDER_DEVELOP
265 #define PIA_RETURN_RESULT_WITH_REPORT(result, ...) \
266  \
267 { \
268  ::nn::pia::common::Report* pPiaReport = ::nn::pia::common::Report::GetGlobalObject(); \
269  if (pPiaReport) \
270  { \
271  uint32_t logLevelError_ = ::nn::pia::common::Report::LevelError; \
272  pPiaReport->WriteCaption(logLevelError_); \
273  pPiaReport->Write(logLevelError_, "[%s] %s %s:", #result, PIA_CODE_POSITION_FILE, PIA_CODE_POSITION_FUNC); \
274  pPiaReport->Write(logLevelError_, __VA_ARGS__); \
275  pPiaReport->Write(logLevelError_, "\n"); \
276  } \
277  else \
278  { \
279  PIA_BASIC_PRINT("[%s] %s %s:", #result, PIA_CODE_POSITION_FILE, PIA_CODE_POSITION_FUNC); \
280  PIA_BASIC_PRINT(__VA_ARGS__); \
281  PIA_BASIC_PRINT("\n"); \
282  } \
283  PIA_RETURN_RESULT(result); \
284  \
285 }
286 #else
287 #define PIA_RETURN_RESULT_WITH_REPORT(result, ...) \
288  { \
289  return result(); \
290  }
291 #endif
292 }
293 }
294 } // end of namespace nn::pia::common