This repository was archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.cpp
More file actions
73 lines (58 loc) · 2.45 KB
/
Copy pathlogging.cpp
File metadata and controls
73 lines (58 loc) · 2.45 KB
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
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017-2018 Dimitry Ishenko
// Contact: dimitry (dot) ishenko (at) (gee) mail (dot) com
//
// Distributed under the GNU GPL license. See the LICENSE.md file for details.
////////////////////////////////////////////////////////////////////////////////
#include "util/logging.hpp"
#include <atomic>
#include <cstdlib>
#include <iostream>
#include <syslog.h>
////////////////////////////////////////////////////////////////////////////////
namespace util
{
////////////////////////////////////////////////////////////////////////////////
namespace { std::atomic<bool> debug_ = std::getenv("DEBUG"); }
void debug(bool enable) noexcept { debug_ = enable; }
bool debug() noexcept { return debug_; }
////////////////////////////////////////////////////////////////////////////////
namespace { std::atomic<bool> trace_ = std::getenv("TRACE"); }
void trace(bool enable) noexcept { trace_ = enable; }
bool trace() noexcept { return trace_; }
////////////////////////////////////////////////////////////////////////////////
namespace { std::atomic<bool> console_ = true; }
void send_to_console(bool enable) noexcept { console_ = enable; }
bool send_to_console() noexcept { return console_; }
////////////////////////////////////////////////////////////////////////////////
namespace { std::atomic<bool> syslog_ = false; }
void send_to_syslog(bool enable) noexcept { syslog_ = enable; }
bool send_to_syslog() noexcept { return syslog_; }
////////////////////////////////////////////////////////////////////////////////
stream::stream(std::string_view tag, level l) : stream(l)
{ if(tag.size()) *this << tag << ": "; }
////////////////////////////////////////////////////////////////////////////////
stream::~stream()
{
std::ostream* os;
int prio;
switch(level_)
{
case level::trc:
if(!trace()) return;
os = &std::cout; prio = LOG_DEBUG;
break;
case level::dbg:
if(!debug()) return;
os = &std::cout; prio = LOG_DEBUG;
break;
case level::info: os = &std::cout; prio = LOG_INFO; break;
case level::warn: os = &std::cerr; prio = LOG_WARNING; break;
case level::err : os = &std::cerr; prio = LOG_ERR; break;
}
auto s = str();
if(send_to_console()) (*os) << s << std::endl;
if(send_to_syslog()) ::syslog(prio, "%s", s.data());
}
////////////////////////////////////////////////////////////////////////////////
}