comparison Core/Logging.h @ 3359:815b81142ff7 emscripten-logging

Enable custom logging functions to redirect to emscripten specific logging calls in the ORTHANC_ENABLE_LOGGING_STDIO mode.
author Benjamin Golinvaux <bgo@osimis.io>
date Tue, 07 May 2019 11:23:11 +0200
parents 54cdad5a7228
children e7723a39adf8
comparison
equal deleted inserted replaced
3355:eb18269de57f 3359:815b81142ff7
27 * General Public License for more details. 27 * General Public License for more details.
28 * 28 *
29 * You should have received a copy of the GNU General Public License 29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. 30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/ 31 **/
32
33
34 #pragma once 32 #pragma once
35 33
36 #include <iostream> 34 #include <iostream>
37 35
38 #if !defined(ORTHANC_ENABLE_LOGGING) 36 #if !defined(ORTHANC_ENABLE_LOGGING)
79 77
80 void EnableInfoLevel(bool enabled); 78 void EnableInfoLevel(bool enabled);
81 79
82 void EnableTraceLevel(bool enabled); 80 void EnableTraceLevel(bool enabled);
83 81
84 #ifdef __EMSCRIPTEN__ 82 bool IsTraceLevelEnabled();
85 // calling this function will change the error_, warning_ and info_ 83
86 // stream objects so that their operator<< writes into the browser 84 bool IsInfoLevelEnabled();
87 // console using emscripten_console_error(), emscripten_console_warn()
88 // and emscripten_console_log()
89 void EnableEmscriptenLogging();
90 #endif
91 85
92 void SetTargetFile(const std::string& path); 86 void SetTargetFile(const std::string& path);
93 87
94 void SetTargetFolder(const std::string& path); 88 void SetTargetFolder(const std::string& path);
89
90 #if ORTHANC_ENABLE_LOGGING_STDIO == 1
91 typedef void (*LoggingFunction)(const char*);
92 void SetErrorWarnInfoTraceLoggingFunctions(
93 LoggingFunction errorLogFunc,
94 LoggingFunction warningLogfunc,
95 LoggingFunction infoLogFunc,
96 LoggingFunction traceLogFunc);
97 #endif
98
95 99
96 struct NullStream : public std::ostream 100 struct NullStream : public std::ostream
97 { 101 {
98 NullStream() : 102 NullStream() :
99 std::ios(0), 103 std::ios(0),
108 } 112 }
109 }; 113 };
110 } 114 }
111 } 115 }
112 116
113
114 #if ORTHANC_ENABLE_LOGGING != 1 117 #if ORTHANC_ENABLE_LOGGING != 1
115 118
116 # define LOG(level) ::Orthanc::Logging::NullStream() 119 # define LOG(level) ::Orthanc::Logging::NullStream()
117 # define VLOG(level) ::Orthanc::Logging::NullStream() 120 # define VLOG(level) ::Orthanc::Logging::NullStream()
118
119 121
120 #elif (ORTHANC_ENABLE_LOGGING_PLUGIN == 1 || \ 122 #elif (ORTHANC_ENABLE_LOGGING_PLUGIN == 1 || \
121 ORTHANC_ENABLE_LOGGING_STDIO == 1) 123 ORTHANC_ENABLE_LOGGING_STDIO == 1)
122 124
123 # include <boost/noncopyable.hpp> 125 # include <boost/noncopyable.hpp>
231 This function is only to be used by tests. 233 This function is only to be used by tests.
232 */ 234 */
233 void DiscardLoggingMemento(LoggingMemento memento); 235 void DiscardLoggingMemento(LoggingMemento memento);
234 236
235 /** 237 /**
236 std::streambuf subclass used in FunctionCallingStream
237 */
238 template<typename T>
239 class FuncStreamBuf : public std::stringbuf
240 {
241 public:
242 FuncStreamBuf(T func) : func_(func) {}
243
244 virtual int sync()
245 {
246 std::string text = this->str();
247 const char* buf = text.c_str();
248 func_(buf);
249 this->str("");
250 return 0;
251 }
252 private:
253 T func_;
254 };
255
256 /**
257 Set custom logging streams for the error, warning and info logs. 238 Set custom logging streams for the error, warning and info logs.
258 This function may not be called if a log file or folder has been 239 This function may not be called if a log file or folder has been
259 set beforehand. All three pointers must be valid and cannot be NULL. 240 set beforehand. All three pointers must be valid and cannot be NULL.
260 241
261 Please ensure the supplied streams remain alive and valid as long as 242 Please ensure the supplied streams remain alive and valid as long as
266 the pointers become invalid. 247 the pointers become invalid.
267 */ 248 */
268 void SetErrorWarnInfoLoggingStreams(std::ostream* errorStream, 249 void SetErrorWarnInfoLoggingStreams(std::ostream* errorStream,
269 std::ostream* warningStream, 250 std::ostream* warningStream,
270 std::ostream* infoStream); 251 std::ostream* infoStream);
252
253 #ifdef __EMSCRIPTEN__
254 /**
255 This function will change the logging streams so that the logging functions
256 provided by emscripten html5.h API functions are used : it will change the
257 error_, warning_ and info_ stream objects so that their operator<< writes
258 into the browser console using emscripten_console_error(),
259 emscripten_console_warn() and emscripten_console_log(). This will allow for
260 logging levels to be correctly handled by the browser when the code executes
261 in Web Assembly
262 */
263 void EnableEmscriptenLogging();
264 #endif
271 } 265 }
272 } 266 }
273 267
274 #endif // ORTHANC_ENABLE_LOGGING 268 #endif // ORTHANC_ENABLE_LOGGING