annotate Resources/Orthanc/Core/Logging.cpp @ 81:8677d95753f8 wasm

switching to Oracle in SDL samples
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 24 May 2017 22:54:09 +0200
parents 81e2651dca17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
40
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
5 * Copyright (C) 2017 Osimis, Belgium
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * License, or (at your option) any later version.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * In addition, as a special exception, the copyright holders of this
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * program give permission to link the code of its release with the
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * the linked executables. You must obey the GNU General Public License
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * your version of the file(s), but you are not obligated to do so. If
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * you do not wish to do so, delete this exception statement from your
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * version. If you delete this exception statement from all source files
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * in the program, then also delete it here.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * This program is distributed in the hope that it will be useful, but
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 * General Public License for more details.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * You should have received a copy of the GNU General Public License
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 **/
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "PrecompiledHeaders.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "Logging.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #if ORTHANC_ENABLE_LOGGING != 1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 namespace Orthanc
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 namespace Logging
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 void Initialize()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 void Finalize()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 void Reset()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 void Flush()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 void EnableInfoLevel(bool enabled)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 void EnableTraceLevel(bool enabled)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 void SetTargetFile(const std::string& path)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 void SetTargetFolder(const std::string& path)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
40
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
77
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
78 #elif ORTHANC_ENABLE_LOGGING_PLUGIN == 1
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
79
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
80 /*********************************************************
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
81 * Logger compatible with the Orthanc plugin SDK
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
82 *********************************************************/
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
83
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
84 #include <boost/lexical_cast.hpp>
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
85
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
86 namespace Orthanc
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
87 {
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
88 namespace Logging
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
89 {
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
90 static OrthancPluginContext* context_ = NULL;
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
91
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
92 void Initialize(OrthancPluginContext* context)
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
93 {
42
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 40
diff changeset
94 context_ = context;
40
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
95 }
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
96
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
97 InternalLogger::InternalLogger(const char* level,
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
98 const char* file /* ignored */,
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
99 int line /* ignored */) :
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
100 level_(level)
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
101 {
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
102 }
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
103
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
104 InternalLogger::~InternalLogger()
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
105 {
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
106 if (context_ != NULL)
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
107 {
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
108 if (level_ == "ERROR")
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
109 {
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
110 OrthancPluginLogError(context_, message_.c_str());
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
111 }
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
112 else if (level_ == "WARNING")
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
113 {
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
114 OrthancPluginLogWarning(context_, message_.c_str());
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
115 }
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
116 else if (level_ == "INFO")
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
117 {
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
118 OrthancPluginLogInfo(context_, message_.c_str());
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
119 }
42
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 40
diff changeset
120 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 40
diff changeset
121 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 40
diff changeset
122 std::string s = "Unknown log level (" + level_ + ") for message: " + message_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 40
diff changeset
123 OrthancPluginLogError(context_, s.c_str());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 40
diff changeset
124 }
40
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
125 }
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
126 }
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
127
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
128 InternalLogger& InternalLogger::operator<< (const std::string& message)
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
129 {
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
130 message_ += message;
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
131 return *this;
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
132 }
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
133
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
134 InternalLogger& InternalLogger::operator<< (const char* message)
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
135 {
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
136 message_ += std::string(message);
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
137 return *this;
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
138 }
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
139
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
140 InternalLogger& InternalLogger::operator<< (int message)
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
141 {
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
142 message_ += boost::lexical_cast<std::string>(message);
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
143 return *this;
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
144 }
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
145 }
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
146 }
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
147
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
148
7207a407bcd8 shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
149 #else /* ORTHANC_ENABLE_LOGGING_PLUGIN == 0 && ORTHANC_ENABLE_LOGGING == 1 */
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 /*********************************************************
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 * Internal logger of Orthanc, that mimics some
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 * behavior from Google Log.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 *********************************************************/
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 #include "OrthancException.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 #include "Enumerations.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 #include "Toolbox.h"
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
159 #include "SystemToolbox.h"
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 #include <fstream>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 #include <boost/filesystem.hpp>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 #include <boost/thread.hpp>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 #if BOOST_HAS_DATE_TIME == 1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 # include <boost/date_time/posix_time/posix_time.hpp>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 #else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 # error Boost::date_time is required
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 namespace
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 struct LoggingContext
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 bool infoEnabled_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 bool traceEnabled_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 std::string targetFile_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 std::string targetFolder_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 std::ostream* error_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 std::ostream* warning_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 std::ostream* info_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 std::auto_ptr<std::ofstream> file_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 LoggingContext() :
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 infoEnabled_(false),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 traceEnabled_(false),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 error_(&std::cerr),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 warning_(&std::cerr),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 info_(&std::cerr)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 };
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 static std::auto_ptr<LoggingContext> loggingContext_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 static boost::mutex loggingMutex_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 namespace Orthanc
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 namespace Logging
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 static void GetLogPath(boost::filesystem::path& log,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 boost::filesystem::path& link,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 const std::string& suffix,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 const std::string& directory)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 /**
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 From Google Log documentation:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 Unless otherwise specified, logs will be written to the filename
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 "<program name>.<hostname>.<user name>.log<suffix>.",
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 followed by the date, time, and pid (you can't prevent the date,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 time, and pid from being in the filename).
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 In this implementation : "hostname" and "username" are not used
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 **/
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 boost::filesystem::path root(directory);
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
227 boost::filesystem::path exe(SystemToolbox::GetPathToExecutable());
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 if (!boost::filesystem::exists(root) ||
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 !boost::filesystem::is_directory(root))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 throw OrthancException(ErrorCode_CannotWriteFile);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 char date[64];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 sprintf(date, "%04d%02d%02d-%02d%02d%02d.%d",
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 static_cast<int>(now.date().year()),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 now.date().month().as_number(),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 now.date().day().as_number(),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 now.time_of_day().hours(),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 now.time_of_day().minutes(),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 now.time_of_day().seconds(),
15
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 12
diff changeset
243 SystemToolbox::GetProcessId());
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 std::string programName = exe.filename().replace_extension("").string();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 log = (root / (programName + ".log" + suffix + "." + std::string(date)));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 link = (root / (programName + ".log" + suffix));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 static void PrepareLogFolder(std::auto_ptr<std::ofstream>& file,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 const std::string& suffix,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 const std::string& directory)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 boost::filesystem::path log, link;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 GetLogPath(log, link, suffix, directory);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 boost::filesystem::remove(link);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 boost::filesystem::create_symlink(log.filename(), link);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 file.reset(new std::ofstream(log.string().c_str()));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 void Initialize()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 boost::mutex::scoped_lock lock(loggingMutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 loggingContext_.reset(new LoggingContext);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 void Finalize()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 boost::mutex::scoped_lock lock(loggingMutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 loggingContext_.reset(NULL);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 void Reset()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 // Recover the old logging context
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 std::auto_ptr<LoggingContext> old;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 boost::mutex::scoped_lock lock(loggingMutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 if (loggingContext_.get() == NULL)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 old = loggingContext_;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 // Create a new logging context,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 loggingContext_.reset(new LoggingContext);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 EnableInfoLevel(old->infoEnabled_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 EnableTraceLevel(old->traceEnabled_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 if (!old->targetFolder_.empty())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 SetTargetFolder(old->targetFolder_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 else if (!old->targetFile_.empty())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 SetTargetFile(old->targetFile_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 void EnableInfoLevel(bool enabled)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 boost::mutex::scoped_lock lock(loggingMutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 assert(loggingContext_.get() != NULL);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 loggingContext_->infoEnabled_ = enabled;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 if (!enabled)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322 // Also disable the "TRACE" level when info-level debugging is disabled
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 loggingContext_->traceEnabled_ = false;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 void EnableTraceLevel(bool enabled)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 boost::mutex::scoped_lock lock(loggingMutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 assert(loggingContext_.get() != NULL);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 loggingContext_->traceEnabled_ = enabled;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 if (enabled)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 // Also enable the "INFO" level when trace-level debugging is enabled
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 loggingContext_->infoEnabled_ = true;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 static void CheckFile(std::auto_ptr<std::ofstream>& f)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 if (loggingContext_->file_.get() == NULL ||
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 !loggingContext_->file_->is_open())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347 throw OrthancException(ErrorCode_CannotWriteFile);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 void SetTargetFolder(const std::string& path)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 boost::mutex::scoped_lock lock(loggingMutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 assert(loggingContext_.get() != NULL);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 PrepareLogFolder(loggingContext_->file_, "" /* no suffix */, path);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 CheckFile(loggingContext_->file_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 loggingContext_->targetFile_.clear();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 loggingContext_->targetFolder_ = path;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361 loggingContext_->warning_ = loggingContext_->file_.get();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 loggingContext_->error_ = loggingContext_->file_.get();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 loggingContext_->info_ = loggingContext_->file_.get();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367 void SetTargetFile(const std::string& path)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 boost::mutex::scoped_lock lock(loggingMutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 assert(loggingContext_.get() != NULL);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 loggingContext_->file_.reset(new std::ofstream(path.c_str(), std::fstream::app));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 CheckFile(loggingContext_->file_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 loggingContext_->targetFile_ = path;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 loggingContext_->targetFolder_.clear();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 loggingContext_->warning_ = loggingContext_->file_.get();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 loggingContext_->error_ = loggingContext_->file_.get();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 loggingContext_->info_ = loggingContext_->file_.get();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 InternalLogger::InternalLogger(const char* level,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 const char* file,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 int line) :
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 lock_(loggingMutex_),
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 stream_(&null_) // By default, logging to "/dev/null" is simulated
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 if (loggingContext_.get() == NULL)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n");
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
395 try
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 {
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
397 LogLevel l = StringToLogLevel(level);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
398
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
399 if ((l == LogLevel_Info && !loggingContext_->infoEnabled_) ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
400 (l == LogLevel_Trace && !loggingContext_->traceEnabled_))
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
401 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
402 // This logging level is disabled, directly exit and unlock
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
403 // the mutex to speed-up things. The stream is set to "/dev/null"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
404 lock_.unlock();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
405 return;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
406 }
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
408 // Compute the header of the line, temporary release the lock as
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
409 // this is a time-consuming operation
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
410 lock_.unlock();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
411 std::string header;
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
413 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
414 boost::filesystem::path path(file);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
415 boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
416 boost::posix_time::time_duration duration = now.time_of_day();
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
418 /**
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
419 From Google Log documentation:
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
421 "Log lines have this form:
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
423 Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg...
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
425 where the fields are defined as follows:
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
427 L A single character, representing the log level (eg 'I' for INFO)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
428 mm The month (zero padded; ie May is '05')
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
429 dd The day (zero padded)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
430 hh:mm:ss.uuuuuu Time in hours, minutes and fractional seconds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
431 threadid The space-padded thread ID as returned by GetTID() (this matches the PID on Linux)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
432 file The file name
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
433 line The line number
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
434 msg The user-supplied message"
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
436 In this implementation, "threadid" is not printed.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
437 **/
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
439 char date[32];
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
440 sprintf(date, "%c%02d%02d %02d:%02d:%02d.%06d ",
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
441 level[0],
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
442 now.date().month().as_number(),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
443 now.date().day().as_number(),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
444 duration.hours(),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
445 duration.minutes(),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
446 duration.seconds(),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
447 static_cast<int>(duration.fractional_seconds()));
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
448
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
449 header = std::string(date) + path.filename().string() + ":" + boost::lexical_cast<std::string>(line) + "] ";
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
450 }
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
453 // The header is computed, we now re-lock the mutex to access
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
454 // the stream objects. Pay attention that "loggingContext_",
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
455 // "infoEnabled_" or "traceEnabled_" might have changed while
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
456 // the mutex was unlocked.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
457 lock_.lock();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
458
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
459 if (loggingContext_.get() == NULL)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
460 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
461 fprintf(stderr, "ERROR: Trying to log a message after the finalization of the logging engine\n");
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
462 return;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
463 }
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
464
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
465 switch (l)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
466 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
467 case LogLevel_Error:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
468 stream_ = loggingContext_->error_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
469 break;
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
471 case LogLevel_Warning:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
472 stream_ = loggingContext_->warning_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
473 break;
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
474
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
475 case LogLevel_Info:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
476 if (loggingContext_->infoEnabled_)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
477 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
478 stream_ = loggingContext_->info_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
479 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
480
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
481 break;
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
483 case LogLevel_Trace:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
484 if (loggingContext_->traceEnabled_)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
485 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
486 stream_ = loggingContext_->info_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
487 }
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
489 break;
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
491 default:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
492 throw OrthancException(ErrorCode_InternalError);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
493 }
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
495 if (stream_ == &null_)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
496 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
497 // The logging is disabled for this level. The stream is the
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
498 // "null_" member of this object, so we can release the global
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
499 // mutex.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
500 lock_.unlock();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
501 }
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
503 (*stream_) << header;
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504 }
12
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
505 catch (...)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
506 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
507 // Something is going really wrong, probably running out of
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
508 // memory. Fallback to a degraded mode.
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
509 stream_ = loggingContext_->error_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
510 (*stream_) << "E???? ??:??:??.?????? ] ";
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1
diff changeset
511 }
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
512 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
513
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515 InternalLogger::~InternalLogger()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
517 if (stream_ != &null_)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
518 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
519 #if defined(_WIN32)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520 *stream_ << "\r\n";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
521 #else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522 *stream_ << "\n";
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523 #endif
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525 stream_->flush();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
527 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
528
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530 void Flush()
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 boost::mutex::scoped_lock lock(loggingMutex_);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534 if (loggingContext_.get() != NULL &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535 loggingContext_->file_.get() != NULL)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
536 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537 loggingContext_->file_->flush();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
538 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
540 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
541 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
542
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
543 #endif // ORTHANC_ENABLE_LOGGING