Skip to content

Commit cc2356e

Browse files
authored
Merge pull request #255 from vizzuhq/logging
Unifying logs
2 parents 3103639 + efcad31 commit cc2356e

File tree

6 files changed

+52
-24
lines changed

6 files changed

+52
-24
lines changed

src/apps/weblib/interface.cpp

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

1717
Interface::Interface() : versionStr(std::string(Main::version))
1818
{
19+
IO::Log::setEnabled(false);
20+
IO::Log::setTimestamp(false);
1921
needsUpdate = false;
20-
logging = false;
2122
eventParam = nullptr;
2223
}
2324

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

306307
void Interface::init()
307308
{
308-
IO::Log::set(
309-
[=, this](const std::string &msg)
310-
{
311-
if (logging) log((msg + "\n").c_str());
312-
});
313-
314309
taskQueue = std::make_shared<GUI::TaskQueue>();
315310
auto&& chartWidget = std::make_shared<UI::ChartWidget>(taskQueue);
316311
chart = {chartWidget, std::addressof(chartWidget->getChart())};
@@ -331,6 +326,11 @@ void Interface::init()
331326
needsUpdate = true;
332327
}
333328

329+
void Interface::setLogging(bool enable)
330+
{
331+
IO::Log::setEnabled(enable);
332+
}
333+
334334
void Interface::poll()
335335
{
336336
if (taskQueue) taskQueue->poll();
@@ -424,5 +424,3 @@ void Interface::keyPress(int key, bool ctrl, bool alt, bool shift)
424424
else
425425
throw std::logic_error("No chart exists");
426426
}
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: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
#include "log.h"
22

3+
#include <ctime>
4+
35
using namespace IO;
46

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

732
void Log::set(Log::LogFunc f) { logFunc = std::move(f); }
833

9-
Log::Log(const std::string &msg)
34+
void Log::setEnabled(bool value) { enabled = value; }
35+
36+
void Log::setTimestamp(bool value) { timestamp = value; }
37+
38+
void Log::print(const std::string &msg)
1039
{
1140
if (logFunc) logFunc(msg);
1241
}

src/base/io/log.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ 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);
20+
static void setTimestamp(bool);
2121

2222
private:
23-
static LogFunc logFunc;
23+
static void print(const std::string &msg);
24+
Log() = default;
25+
~Log() = default;
26+
27+
friend class LogRecord;
2428
};
2529

2630
class LogRecord
2731
{
2832
public:
29-
LogRecord() = default;
30-
LogRecord(LogRecord &&) = default;
31-
~LogRecord() { Log{content}; }
33+
~LogRecord() { Log::print(content); }
3234

3335
template <typename T> LogRecord &operator<<(const T &value)
3436
{
@@ -37,6 +39,11 @@ class LogRecord
3739
}
3840

3941
private:
42+
friend LogRecord log();
43+
44+
LogRecord();
45+
LogRecord(LogRecord &&) = default;
46+
4047
std::string content;
4148
};
4249

0 commit comments

Comments
 (0)