Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarLogging.hpp
blob: 4f93d8a39c006ca2340f94abded8f75db984a3d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#pragma once

#include "StarThread.hpp"
#include "StarSet.hpp"
#include "StarString.hpp"
#include "StarPoly.hpp"
#include "StarBiMap.hpp"
#include "StarTime.hpp"
#include "StarFile.hpp"

namespace Star {

enum class LogLevel {
  Debug,
  Info,
  Warn,
  Error
};
extern EnumMap<LogLevel> const LogLevelNames;

STAR_CLASS(LogSink);

// A sink for Logger messages.
class LogSink {
public:
  LogSink();
  virtual ~LogSink();

  virtual void log(char const* msg, LogLevel level) = 0;

  void setLevel(LogLevel level);
  LogLevel level();

private:
  atomic<LogLevel> m_level;
};

class StdoutLogSink : public LogSink {
public:
  virtual void log(char const* msg, LogLevel level);

private:
  Mutex m_logMutex;
};

class FileLogSink : public LogSink {
public:
  FileLogSink(String const& filename, LogLevel level, bool truncate);

  virtual void log(char const* msg, LogLevel level);

private:
  FilePtr m_output;
  Mutex m_logMutex;
};

// A basic loging system that logs to multiple streams.  Can log at Debug,
// Info, Warn, and Error logging levels.  By default logs to stdout.
class Logger {
public:
  static void addSink(LogSinkPtr s);
  static void removeSink(LogSinkPtr s);

  // Default LogSink that outputs to stdout.
  static LogSinkPtr stdoutSink();
  // Don't use the stdout sink.
  static void removeStdoutSink();

  static void log(LogLevel level, char const* msg);

  template <typename... Args>
  static void logf(LogLevel level, char const* msg, Args const&... args);

  template <typename... Args>
  static void debug(char const* msg, Args const&... args);
  template <typename... Args>
  static void info(char const* msg, Args const&... args);
  template <typename... Args>
  static void warn(char const* msg, Args const&... args);
  template <typename... Args>
  static void error(char const* msg, Args const&... args);

  static bool loggable(LogLevel level);
  static void refreshLoggable();
private:

  static shared_ptr<StdoutLogSink> s_stdoutSink;
  static HashSet<LogSinkPtr> s_sinks;
  static Array<bool, 4> s_loggable;
  static Mutex s_mutex;
};

// For logging data that is very high frequency. It is a map of debug values to
// be displayed every frame, or in a debug output window, etc.
class LogMap {
public:
  static String getValue(String const& key);
  static void setValue(String const& key, String const& value);

  // Shorthand, converts given type to string using std::ostream.
  template <typename T>
  static void set(String const& key, T const& t);

  static Map<String, String> getValues();
  static void clear();

private:
  static HashMap<String, String> s_logMap;
  static Mutex s_logMapMutex;
};

// Logging for spatial data.  Divided into multiple named coordinate spaces.
class SpatialLogger {
public:
  // Maximum count of objects stored per space
  static size_t const MaximumLines = 200000;
  static size_t const MaximumPoints = 200000;
  static size_t const MaximumText = 10000;

  struct Line {
    Vec2F begin;
    Vec2F end;
    Vec4B color;
  };

  struct Point {
    Vec2F position;
    Vec4B color;
  };

  struct LogText {
    String text;
    Vec2F position;
    Vec4B color;
  };

  static void logPoly(char const* space, PolyF const& poly, Vec4B const& color);
  static void logLine(char const* space, Line2F const& line, Vec4B const& color);
  static void logLine(char const* space, Vec2F const& begin, Vec2F const& end, Vec4B const& color);
  static void logPoint(char const* space, Vec2F const& position, Vec4B const& color);
  static void logText(char const* space, String text, Vec2F const& position, Vec4B const& color);

  static Deque<Line> getLines(char const* space, bool andClear);
  static Deque<Point> getPoints(char const* space, bool andClear);
  static Deque<LogText> getText(char const* space, bool andClear);

  static void clear();

  static bool observed();
  static void setObserved(bool observed);

private:
  static Mutex s_mutex;
  static StringMap<Deque<Line>> s_lines;
  static StringMap<Deque<Point>> s_points;
  static StringMap<Deque<LogText>> s_logText;
  static bool s_observed;
};

template <typename... Args>
void Logger::logf(LogLevel level, char const* msg, Args const&... args) {
  if (loggable(level)) {
    std::string output = strf(msg, args...);
    MutexLocker locker(s_mutex);
    for (auto const& l : s_sinks) {
      if (l->level() <= level) {
        l->log(output.c_str(), level);
      }
    }
  }
}

template <typename... Args>
void Logger::debug(char const* msg, Args const&... args) {
  logf(LogLevel::Debug, msg, args...);
}

template <typename... Args>
void Logger::info(char const* msg, Args const&... args) {
  logf(LogLevel::Info, msg, args...);
}

template <typename... Args>
void Logger::warn(char const* msg, Args const&... args) {
  logf(LogLevel::Warn, msg, args...);
}

template <typename... Args>
void Logger::error(char const* msg, Args const&... args) {
  logf(LogLevel::Error, msg, args...);
}

template <typename T>
void LogMap::set(String const& key, T const& t) {
  setValue(key, toString(t));
}

}