Develop Biology
The language of life
Engine.cpp
Go to the documentation of this file.
1/*
2 * This file is a part of the Biology project by eons LLC.
3 * Biology (aka Develop Biology) is a framework for approaching software
4 * development from a natural sciences perspective.
5 *
6 * Copyright (C) 2021 Séon O'Shannon & eons LLC
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#include "bio/log/Engine.h"
27#include "bio/physical/Time.h"
28#include <cstring>
29#include <stdarg.h>
30#include <ios>
31#include <sstream>
32#include <cstdio>
33
34namespace bio {
35namespace log {
36
38{
39 //Set all filters to only log if level is >= Info
40 m_levelFilter.assign(
41 FilterPerspective::Instance().GetNumUsedIds(),
42 level::Info());
43}
44
46{
47
48}
49
51 Filter filter,
52 Level level,
53 const char* format,
54 va_list args
55)
56{
58 filter,
59 level
60 ), ,
61 return);
62
63 char str[BIO_LOG_PRINTF_MAX_LINE_SIZE + 1];
64
65 vsnprintf(
66 str,
67 sizeof(str),
68 format,
69 args
70 );
71 va_end(args);
73
74 m_logMessage.clear();
75 m_logMessage.str(""); //TODO: is seekp good enough? what is faster?
76
77 m_logMessage << physical::GetCurrentTimestamp() << " " << FilterPerspective::Instance().GetNameFromId(filter) << " " << LevelPerspective::Instance().GetNameFromId(level) << ": " << str << "\n";
78 Output(m_logMessage.str());
79}
80
82 Filter filter,
83 Level level,
84 const char* format,
85 ...
86)
87{
88 if (FilterPass(
89 filter,
90 level
91 ))
92 {
93 return; //filter off, don't log
94 }
95
96 va_list args;
97 va_start(args,
98 format);
99 Log(
100 filter,
101 level,
102 format,
103 args
104 );
105 va_end(args);
106}
107
109 Filter filter,
110 Level level
111) const
112{
113 return level >= m_levelFilter[filter];
114}
115
117 Filter filter,
118 Level level
119)
120{
121 m_levelFilter[filter] = level;
122 return true; //SUCCESS
123}
124
126 Name filter,
127 Name level
128)
129{
130 return FilterSet(
131 FilterPerspective::Instance().GetIdFromName(filter),
132 LevelPerspective::Instance().GetIdFromName(level));
133}
134
135Level Engine::FilterGet(Filter filter) const
136{
137 return m_levelFilter[filter];
138}
139
140} //log namespace
141} //bio namespace
#define BIO_SANITIZE_AT_SAFETY_LEVEL_2(test, success, failure)
Level FilterGet(Filter filter) const
Definition: Engine.cpp:135
std::ostringstream m_logMessage
Definition: Engine.h:131
virtual void Output(const std::string &logString)=0
virtual ~Engine()
Definition: Engine.cpp:45
bool FilterPass(Filter filter, Level level) const
Definition: Engine.cpp:108
bool FilterSet(Filter filter, Level level)
Definition: Engine.cpp:116
void Log(Filter filter, Level level, const char *format, va_list args)
Definition: Engine.cpp:50
#define BIO_LOG_PRINTF_MAX_LINE_SIZE
Definition: Macros.h:24
Timestamp GetCurrentTimestamp()
Definition: Time.cpp:41
Definition: Cell.h:31
const char * Name
Definition: Types.h:46