OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 #ifndef DEBUGGER_CORE_DEBUG_LOGGER_H_ | |
5 #define DEBUGGER_CORE_DEBUG_LOGGER_H_ | |
6 #include <stdarg.h> | |
7 #include <stdio.h> | |
8 | |
9 namespace debug { | |
10 | |
11 /// /brief Class to log internal debugger messages, used for debugging, | |
12 /// troubleshooting and testing. | |
13 /// | |
14 /// Prints messages on stdout. Not thread safe. | |
15 class Logger { | |
16 public: | |
17 /// Initialize active logger. | |
18 /// | |
19 /// Does not take ownership of |logger|. | |
20 /// Can be called multiple times. | |
21 static void SetGlobalLogger(Logger* logger); | |
22 | |
23 /// @return active logger. | |
24 static Logger* Get(); | |
25 | |
26 virtual ~Logger() {} | |
27 | |
28 /// Adds message to the log. | |
29 /// Message size limit is ~32K. | |
30 /// @param id unique id of the message, shall have prefixes "TR" - for | |
31 /// tracees, "INF" for information, "WARN" for warnings and "ERR" for errors. | |
32 /// @param fmt string that contains the text to be written to log | |
33 /// @param ... additional arguments as specified in |fmt| | |
34 virtual void Log(const char* id, | |
35 const char* fmt, | |
36 ...); | |
37 | |
38 /// Adds message to the log. | |
39 /// Message size limit is ~32K. | |
40 /// @param id unique id of the message, shall have prefixes "TR" - for | |
41 /// traces, "INF" for information, "WARN" for warnings and "ERR" for errors. | |
42 /// @param fmt string that contains the text to be written to log | |
43 /// @param args additional arguments as specified in |fmt| | |
44 virtual void VLog(const char* id, | |
45 const char* fmt, | |
46 va_list args); | |
47 | |
48 protected: | |
49 /// Outputs record begin marker, current time and |id|. | |
50 virtual void StartRecord(const char* id); | |
51 | |
52 /// Outputs |msg| as is. | |
53 virtual void LogString(const char* msg); | |
54 | |
55 /// Outputs record end marker, flushes the stream. | |
56 virtual void FinishRecord(); | |
57 | |
58 private: | |
59 static Logger* logger_; | |
60 }; | |
61 | |
62 /// Writes messages in specified file (and stdout - optional). Not thread safe. | |
63 /// | |
64 /// Call |Open| before logging. | |
65 class TextFileLogger : public Logger { | |
66 public: | |
67 TextFileLogger(); | |
68 virtual ~TextFileLogger(); | |
69 | |
70 /// Opens file in 'append' mode. | |
71 bool Open(const char* file_name); | |
72 | |
73 void EnableStdout(bool en) { stdout_enabled_ = en; } | |
74 | |
75 protected: | |
76 virtual void LogString(const char* msg); | |
77 virtual void FinishRecord(); | |
78 | |
79 FILE* file_; | |
80 bool stdout_enabled_; | |
81 | |
82 private: | |
83 TextFileLogger(const TextFileLogger&); // DISALLOW_COPY_AND_ASSIGN | |
84 void operator=(const TextFileLogger&); | |
85 }; | |
86 | |
87 } // namespace debug | |
88 | |
89 /// Inline function - replacement for similar macro. | |
90 /// Safe to call even if logger is not there. | |
91 /// Not thread safe. | |
92 inline void DBG_LOG(const char* id, | |
93 const char* fmt, | |
94 ... ) { | |
95 if (NULL != debug::Logger::Get()) { | |
96 va_list marker; | |
97 va_start(marker, fmt); | |
98 debug::Logger::Get()->VLog(id, fmt, marker); | |
99 } | |
100 } | |
101 | |
102 #endif // DEBUGGER_CORE_DEBUG_LOGGER_H_ | |
103 | |
OLD | NEW |