Honeycomb  0.1
Component-Model Framework
Log.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Graph/Dep.h"
6 
7 namespace honey
8 {
9 
11 #define Log_debug Log::inst() << log::level::debug << "[" << log::srcFilename(__FILE__) << ":" << __LINE__ << "] "
12 
14 namespace log
15 {
18 
20  namespace level
21  {
22  extern Level critical;
23  extern Level error;
24  extern Level warning;
25  extern Level info;
26  extern Level debug;
27  }
28 
30  String format(const Level& level, const String& record);
31 
32  struct Sink : SharedObj<Sink>
33  {
35 
36  virtual void operator()(const Level& level, const String& record) = 0;
37  };
38 
40  struct BufferSink : Sink
41  {
43 
44  virtual void operator()(const Level& level, const String& record);
45  vector<tuple<const Level*, String>> records;
46  };
47 
49  struct StreamSink : Sink
50  {
52 
53  StreamSink(ostream& os) : os(os) {}
54  virtual void operator()(const Level& level, const String& record);
55  ostream& os;
56  };
57 
60  {
62 
64  ~FileSink();
65  virtual void operator()(const Level& level, const String& record);
67  std::ofstream os;
68  };
69 
71  inline String srcFilename(const String& path) { szt pos = path.find_last_of(String("\\/")); return pos != String::npos ? path.substr(pos+1) : path; }
72 }
73 
75 class Log
76 {
77 public:
79  typedef unordered_map<Id, log::Sink::Ptr> SinkMap;
80 
82  struct RecordStream : ostringstream
83  {
84  RecordStream(Log& log, const log::Level& level) : log(&log), lock(log._lock) { log._level = &level; }
85  RecordStream(RecordStream&& rhs) : ostringstream(std::move(rhs)), log(rhs.log), lock(std::move(rhs.lock)) { rhs.log = nullptr; }
86  ~RecordStream() { if (!log) return; log->push(str()); }
87  Log* log;
89  };
90 
92  static mt_global(Log, inst,);
93 
95  Log();
96 
98  void addLevel(const log::Level& level);
99  void removeLevel(const log::Level& level);
100  const LevelGraph& levels() const { return _levelGraph; }
101 
103  void addSink(const Id& name, const log::Sink::Ptr& sink);
104  void removeSink(const Id& name);
105  const SinkMap& sinks() const { return _sinks; }
106 
108 
115  void filter(const Id& sink, const vector<const log::Level*>& includes, bool includeDeps = true,
116  const vector<const log::Level*>& excludes = {}, bool excludeDeps = true);
117  void clearFilter(const Id& sink);
118 
120  RecordStream operator<<(const log::Level& level) { return RecordStream(*this, level); }
121 
123  SpinLock::Scoped lock() { return _lock; }
124 
125 private:
126  typedef unordered_map<Id, std::set<Id>> FilterMap;
127 
128  void push(const String& record);
129 
130  LevelGraph _levelGraph;
131  const log::Level* _level;
132  SinkMap _sinks;
133  FilterMap _filters;
134  SpinLock _lock;
135 };
136 
137 }
Formats record to a file stream.
Definition: Log.h:59
~RecordStream()
Definition: Log.h:86
Logger.
Definition: Log.h:75
A thread lock where the lock is acquired through a busy wait loop.
Definition: Spin.h:17
Combined intrusive/non-intrusive smart pointer. Can reference and share any object automatically...
Definition: SharedPtr.h:175
virtual void operator()(const Level &level, const String &record)=0
Level info(nullptr,"info")
General information.
Definition: Log.h:25
RecordStream(Log &log, const log::Level &level)
Definition: Log.h:84
FileSink(String filepath)
Definition: Log.cpp:52
void removeLevel(const log::Level &level)
Definition: Log.cpp:97
std::ofstream os
Definition: Log.h:67
Level debug(nullptr,"debug")
Low-level information for debugging purposes.
Definition: Log.h:26
const LevelGraph & levels() const
Definition: Log.h:100
STL namespace.
static mt_global(Log, inst,)
Get singleton.
SharedPtr< BufferSink > Ptr
Definition: Log.h:42
ostream & os
Definition: Log.h:55
StreamSink(ostream &os)
Definition: Log.h:53
virtual void operator()(const Level &level, const String &record)
Definition: Log.cpp:41
String substr(szt pos=0, szt len=npos) const
Definition: String.h:104
Reference-counted object for intrusive shared pointers.
Definition: SharedPtr.h:93
SpinLock::Scoped lock()
Acquire lock to synchronize output to sinks, other loggers should call this before outputting to the ...
Definition: Log.h:123
Formats record to a stream.
Definition: Log.h:49
Captures records in a buffer.
Definition: Log.h:40
void removeSink(const Id &name)
Definition: Log.cpp:107
Level critical(nullptr,"critical")
Information describing a critical problem that has occurred.
Definition: Log.h:22
Level error(nullptr,"error")
Information describing a major problem that has occurred.
Definition: Log.h:23
virtual void operator()(const Level &level, const String &record)
Definition: Log.cpp:46
Level warning(nullptr,"warning")
Information describing a minor problem that has occurred.
Definition: Log.h:24
SharedPtr< StreamSink > Ptr
Definition: Log.h:51
void clearFilter(const Id &sink)
Definition: Log.cpp:133
vector< tuple< const Level *, String > > records
Definition: Log.h:45
Dependency node for insertion into graph.
Definition: Dep.h:14
Unicode UTF-16 string class, wrapper around std::u16string.
Definition: String.h:23
void addSink(const Id &name, const log::Sink::Ptr &sink)
Add a sink to receive records.
Definition: Log.cpp:102
~FileSink()
Definition: Log.cpp:61
Log()
Create logger with default levels and a standard streams sinks ("stdout" and "stderr") ...
Definition: Log.cpp:75
SpinLock::Scoped lock
Definition: Log.h:88
size_t szt
Size type, shorthand for size_t.
Definition: Core.h:90
String filepath
Definition: Log.h:66
const SinkMap & sinks() const
Definition: Log.h:105
unordered_map< Id, log::Sink::Ptr > SinkMap
Definition: Log.h:79
virtual void operator()(const Level &level, const String &record)
Definition: Log.cpp:67
DepGraph< const log::Level > LevelGraph
Definition: Log.h:78
RecordStream operator<<(const log::Level &level)
Push a record with level to all sinks.
Definition: Log.h:120
String srcFilename(const String &path)
Get filename from source path provided by macro FILE
Definition: Log.h:71
SharedPtr< Sink > Ptr
Definition: Log.h:34
SharedPtr< FileSink > Ptr
Definition: Log.h:61
void addLevel(const log::Level &level)
Add a severity level to categorize records.
Definition: Log.cpp:92
Log * log
Definition: Log.h:87
RecordStream(RecordStream &&rhs)
Definition: Log.h:85
Holds a name string and its hashed value for fast comparison ops. See String Identifier.
Definition: Id.h:25
Definition: Log.h:32
Builds a record.
Definition: Log.h:82
Global Honeycomb namespace.
DepNode< void *, NameId > Level
Severity level.
Definition: Log.h:17
void filter(const Id &sink, const vector< const log::Level * > &includes, bool includeDeps=true, const vector< const log::Level * > &excludes={}, bool excludeDeps=true)
Add a record filter to a sink.
Definition: Log.cpp:113
String format(const Level &level, const String &record)
Format record with date and level id.
Definition: Log.cpp:31