RMS SDK for C++  0.2.1
A client library for using Microsoft RMS from Linux.
Logger.h
1 /*
2  * ======================================================================
3  * Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
4  * Licensed under the MIT License.
5  * See LICENSE.md in the project root for license information.
6  * ======================================================================
7 */
8 
9 #ifndef LOGGER_H
10 #define LOGGER_H
11 
12 #include <cstdio>
13 #include <fstream>
14 #include "types.h"
15 #include "rmsauthExport.h"
16 
17 namespace rmsauth {
18 
19 class Logger
20 {
21 public:
22  template<typename T, typename... Args>
23  static void record(const String& category, const String& tag, const String& record, T value, Args... args);
24 
25  template<typename T, typename... Args>
26  static void info(const String& tag, const String& record, T value, Args... args);
27 
28  template<typename T, typename... Args>
29  static void warning(const String& tag, const String& record, T value, Args... args);
30 
31 
32  template<typename T, typename... Args>
33  static void error(const String& tag, const String& record, T value, Args... args);
34 
35  static void record(const String& category, const String& tag, const String& record);
36  static void info(const String& tag, const String& record);
38  static void hidden(const String& tag, const String& record);
39  static void warning(const String& tag, const String& record);
40  static void error(const String& tag, const String& record);
41  virtual ~Logger(){}
42 
43 protected:
44  virtual void append(const String& category, const String& tag, const String& record) = 0;
45 
46 private:
47  static Logger& instance();
48 
49  static void printf(StringStream& ss, const char *s);
50 
51  template<typename T, typename... Args>
52  static void printf(StringStream& ss, const char *s, T value, Args... args);
53 };
54 
55 template<typename T, typename... Args>
56 void Logger::record(const String& category, const String& tag, const String& record, T value, Args... args)
57 {
58  StringStream ss;
59  Logger::printf(ss, record.c_str(), value, args...);
60  Logger::instance().append(category, tag, ss.str());
61 }
62 
63 template<typename T, typename... Args>
64 void Logger::info(const String& tag, const String& record, T value, Args... args)
65 {
66  Logger::record("INF", tag, record, value, args...);
67 }
68 
69 template<typename T, typename... Args>
70 void Logger::warning(const String& tag, const String& record, T value, Args... args)
71 {
72  Logger::record("WRN", tag, record, value, args...);
73 }
74 
75 template<typename T, typename... Args>
76 void Logger::error(const String& tag, const String& record, T value, Args... args)
77 {
78  Logger::record("ERR", tag, record, value, args...);
79 }
80 
81 template<typename T, typename... Args>
82 void Logger::printf(StringStream& ss, const char *s, T value, Args... args)
83 {
84  while (*s)
85  {
86  if (*s == '%')
87  {
88  if (*(s + 1) == '%')
89  {
90  ++s;
91  }
92  else
93  {
94  ss << value;
95  s += 1;
96  Logger::printf(ss, s, args...); // call even when *s == 0 to detect extra arguments
97  return;
98  }
99  }
100  ss << *s++;
101  }
102 }
103 
104 class LoggerImpl : public Logger
105 {
106 public:
107  ~LoggerImpl();
108 
109 protected:
110  virtual void append(const String& category, const String& tag, const String& record) override;
111  static String getLocalTime(const String& format);
112 
113 private:
114  LoggerImpl();
115  friend class Logger;
116  std::ofstream stream_;
117 };
118 
119 } // namespace rmsauth {
120 
121 #endif // LOGGER_H
Definition: Logger.h:19
Definition: Logger.h:104
static void hidden(const String &tag, const String &record)
to enable these records - set environment variable: export RMS_HIDDEN_LOG=ON
Definition: AcquireTokenForClientHandler.h:14