Skip to content

Commit f0ea885

Browse files
committed
Unify logging
1 parent aba3773 commit f0ea885

File tree

6 files changed

+42
-24
lines changed

6 files changed

+42
-24
lines changed

src/apps/weblib/interface.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ Interface Interface::instance;
1616

1717
Interface::Interface() : versionStr(std::string(Main::version))
1818
{
19+
IO::Log::setEnabled(false);
1920
needsUpdate = false;
20-
logging = false;
2121
eventParam = nullptr;
2222
}
2323

@@ -305,12 +305,6 @@ const char *Interface::dataMetaInfo()
305305

306306
void Interface::init()
307307
{
308-
IO::Log::set(
309-
[=, this](const std::string &msg)
310-
{
311-
if (logging) log((msg + "\n").c_str());
312-
});
313-
314308
taskQueue = std::make_shared<GUI::TaskQueue>();
315309
auto&& chartWidget = std::make_shared<UI::ChartWidget>(taskQueue);
316310
chart = {chartWidget, std::addressof(chartWidget->getChart())};
@@ -331,6 +325,11 @@ void Interface::init()
331325
needsUpdate = true;
332326
}
333327

328+
void Interface::setLogging(bool enable)
329+
{
330+
IO::Log::setEnabled(enable);
331+
}
332+
334333
void Interface::poll()
335334
{
336335
if (taskQueue) taskQueue->poll();
@@ -424,5 +423,3 @@ void Interface::keyPress(int key, bool ctrl, bool alt, bool shift)
424423
else
425424
throw std::logic_error("No chart exists");
426425
}
427-
428-
void Interface::log(const char *str) { jsconsolelog(str); }

src/apps/weblib/interface.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Interface
2020
Interface();
2121
const char *version() const;
2222
void init();
23-
void setLogging(bool enable) { logging = enable; }
23+
void setLogging(bool enable);
2424
void keyPress(int key, bool ctrl, bool alt, bool shift);
2525
void pointerMove(int pointerId, double x, double y);
2626
void pointerDown(int pointerId, double x, double y);
@@ -94,8 +94,6 @@ class Interface
9494
ObjectRegistry objects;
9595
Util::EventDispatcher::Params *eventParam;
9696
bool needsUpdate;
97-
bool logging;
98-
void log(const char *str);
9997
};
10098

10199
}

src/apps/weblib/interface.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
mergeInto(LibraryManager.library, {
2-
jsconsolelog: function(str) {
3-
console.log(new Date().toISOString() + ': ' + UTF8ToString(str));
4-
},
52
openUrl: function(url) {
63
window.open(UTF8ToString(url), '_blank');
74
},

src/apps/weblib/interfacejs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define INTERFACEJS_H
33

44
extern "C" {
5-
extern void jsconsolelog(const char *);
65
extern void openUrl(const char *);
76
extern void setCursor(const char *cursor);
87
extern void event_invoked(int, const char *);

src/base/io/log.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,32 @@
22

33
using namespace IO;
44

5-
Log::LogFunc Log::logFunc;
5+
namespace
6+
{
7+
static bool enabled = true;
8+
static Log::LogFunc logFunc = [](std::string const &s)
9+
{
10+
puts(s.c_str());
11+
};
12+
13+
}
14+
15+
LogRecord::LogRecord() : content("[vizzu] [YYYY-mm-ddTHH:MM:SSZ] ")
16+
{
17+
std::time_t now;
18+
std::time(&now);
19+
std::strftime(content.data() + 9,
20+
21,
21+
"%Y-%m-%dT%H:%M:%SZ",
22+
std::gmtime(&now));
23+
content[9 + 20] = ']';
24+
}
625

726
void Log::set(Log::LogFunc f) { logFunc = std::move(f); }
827

9-
Log::Log(const std::string &msg)
28+
void Log::setEnabled(bool value) { enabled = value; }
29+
30+
void Log::print(const std::string &msg)
1031
{
1132
if (logFunc) logFunc(msg);
1233
}

src/base/io/log.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ class Log
1515
{
1616
public:
1717
typedef std::function<void(const std::string &)> LogFunc;
18-
1918
static void set(LogFunc f);
20-
Log(const std::string &msg);
19+
static void setEnabled(bool);
2120

2221
private:
23-
static LogFunc logFunc;
22+
static void print(const std::string &msg);
23+
Log() = default;
24+
~Log() = default;
25+
26+
friend class LogRecord;
2427
};
2528

2629
class LogRecord
2730
{
2831
public:
29-
LogRecord() = default;
30-
LogRecord(LogRecord &&) = default;
31-
~LogRecord() { Log{content}; }
32+
~LogRecord() { Log::print(content); }
3233

3334
template <typename T> LogRecord &operator<<(const T &value)
3435
{
@@ -37,6 +38,11 @@ class LogRecord
3738
}
3839

3940
private:
41+
friend LogRecord log();
42+
43+
LogRecord();
44+
LogRecord(LogRecord &&) = default;
45+
4046
std::string content;
4147
};
4248

0 commit comments

Comments
 (0)