BAOS .net SDK  18.2.0
kdriveLogger.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2002-2018 WEINZIERL ENGINEERING GmbH
3 // All rights reserved.
4 //
5 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
8 // SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
9 // WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
10 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
11 //
12 
13 #pragma once
14 #include "kdriveManagedConfig.h"
15 #include <kdrive/utility/LoggerFormatter.h>
16 #include <Poco/Logger.h>
17 #include <Poco/Channel.h>
18 #include <Poco/Message.h>
19 #include <Poco/Mutex.h>
20 #include <Poco/AutoPtr.h>
21 #using <mscorlib.dll>
22 
23 namespace unmanaged
24 {
25 
29 struct ConsoleChannel : public Poco::Channel
30 {
31  virtual void log(const Poco::Message& msg);
32  static Poco::FastMutex mutex_;
33 };
34 
35 } // end namespace unmanaged
36 
37 namespace kdrive
38 {
39 
53 public ref class Logger
54 {
55 public:
56  enum class Level
57  {
58  None = 0,
59  Fatal,
60  Critical,
61  Error,
62  Warning,
63  Notice,
64  Information,
65  Debug,
66  Trace
67  };
68 
69  static void SetConsoleChannel()
70  {
71  try
72  {
73  kdrive::utility::LoggerFormatter loggerFormatter;
74  Poco::AutoPtr<unmanaged::ConsoleChannel> channel = new unmanaged::ConsoleChannel;
75  loggerFormatter.initLogger(channel);
76  }
78  }
79 
80  static void SetFileChannel()
81  {
82  try
83  {
84  INIT_ROOT_FILE_LOGGER();
85  }
87  }
88 
89  static void SetFileChannel(System::String^ fileName)
90  {
91  try
92  {
93  std::string s = interop::StringConvA(fileName);
94  kdrive::utility::LoggerFormatter loggerFormatter;
95  loggerFormatter.initRootFileLogger(s);
96  poco_information(Poco::Logger::root(), "File Logger Started");
97  }
99  }
100 
101  static void Log(Level level, System::String^ message)
102  {
103  try
104  {
105  std::string s = interop::StringConvA(message);
106 
107  switch (level)
108  {
109  case Level::None:
110  break;
111 
112  case Level::Fatal:
113  poco_fatal(logger(), s);
114  break;
115 
116  case Level::Critical:
117  poco_critical(logger(), s);
118  break;
119 
120  case Level::Error:
121  poco_error(logger(), s);
122  break;
123 
124  case Level::Warning:
125  poco_warning(logger(), s);
126  break;
127 
128  case Level::Notice:
129  poco_notice(logger(), s);
130  break;
131 
132  case Level::Information:
133  poco_information(logger(), s);
134  break;
135 
136  case Level::Debug:
137  poco_debug(logger(), s);
138  break;
139 
140  case Level::Trace:
141  poco_trace(logger(), s);
142  break;
143 
144  default:
145  poco_information(logger(), s);
146  break;
147  }
148  }
150  }
151 
152  static void Dump(Level level, System::String^ message, array<unsigned char>^ data)
153  {
154  try
155  {
156  std::string s = interop::StringConvA(message);
157  if (data->Length)
158  {
159  pin_ptr<unsigned char> pp = &data[0];
160  const unsigned char* begin = pp;
161  std::vector<unsigned char> v(begin, begin + data->Length);
162  logger().dump(s, &v.at(0), v.size(), static_cast<Poco::Message::Priority>((int)level));
163  }
164  else
165  {
166  logger().dump(s, 0, 0, static_cast<Poco::Message::Priority>((int)level));
167  }
168  }
170  }
171 
172  static void Shutdown()
173  {
174  try
175  {
176  Poco::Logger::shutdown();
177  }
179  }
180 
181  static void SetLevels(Level level)
182  {
183  try
184  {
185  Poco::Logger::setLevel("", static_cast<int>(level));
186  Poco::Logger::root().setLevel(static_cast<int>(level));
187  }
189  }
190 
191 private:
192  /*
193  make the constructor private, because it's not needed. It's an static holder typ
194  */
195  Logger::Logger()
196  {
197  }
198 
199  static Poco::Logger& logger()
200  {
201  return Poco::Logger::get("kdrive.dotnet");
202  }
203 };
204 
205 } // end namespace kdrive
206 
Definition: kdriveManagedInterop.h:39
static void Shutdown()
Definition: kdriveLogger.h:172
static void Dump(Level level, System::String^message, array< unsigned char >^data)
Definition: kdriveLogger.h:152
static void SetLevels(Level level)
Definition: kdriveLogger.h:181
static void SetConsoleChannel()
Definition: kdriveLogger.h:69
Definition: BaosConnection.h:31
Maps the Poco::Logger to the System::Console
Definition: kdriveLogger.h:29
static void SetFileChannel()
Definition: kdriveLogger.h:80
static void Log(Level level, System::String^message)
Definition: kdriveLogger.h:101
#define WZ_KDRIVE_CLR_DEFAULT_CATCH
Definition: kdriveManagedInterop.h:161
virtual void log(const Poco::Message &msg)
Definition: kdriveLogger.cpp:20
static void SetFileChannel(System::String^fileName)
Definition: kdriveLogger.h:89
Level
Definition: kdriveLogger.h:56
Definition: BaosConnection.h:22
Start/Stop the logging system
Definition: kdriveLogger.h:53
static Poco::FastMutex mutex_
Definition: kdriveLogger.h:32