From 4acab04895bf6de37bd2e9037f4aaef3494c5906 Mon Sep 17 00:00:00 2001 From: peri4 Date: Thu, 19 Sep 2024 17:26:58 +0300 Subject: [PATCH] PILog ready to use --- libs/main/application/pilog.cpp | 32 +++++++++++++++++++---------- libs/main/application/pilog.h | 27 ++++++++++++++++++++++-- libs/main/console/pikbdlistener.cpp | 6 +++++- libs/main/console/pikbdlistener.h | 2 ++ 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/libs/main/application/pilog.cpp b/libs/main/application/pilog.cpp index f948d78d..27fece16 100644 --- a/libs/main/application/pilog.cpp +++ b/libs/main/application/pilog.cpp @@ -92,8 +92,10 @@ PILog::~PILog() { void PILog::setDir(const PIString & d) { stopAndWait(); log_dir = d; - PIDir::make(log_dir); - newFile(); + if (output[File]) { + PIDir::make(log_dir); + newFile(); + } start(); } @@ -164,7 +166,6 @@ PICout PILog::makePICout(PIObject * context, Level cat) { void PILog::enqueue(const PIString & msg, Level cat) { - if (log_file.isClosed()) return; auto t = PIDateTime::fromSystemTime(PISystemTime::current()); PIMutexLocker ml(log_mutex); queue.enqueue({cat, t, msg}); @@ -190,9 +191,11 @@ void PILog::newFile() { void PILog::run() { - if (split_tm.elapsed() >= split_time) { - split_tm.reset(); - newFile(); + if (output[File]) { + if (split_tm.elapsed() >= split_time) { + split_tm.reset(); + newFile(); + } } log_mutex.lock(); if (queue.isEmpty()) { @@ -210,10 +213,17 @@ void PILog::run() { auto qi = queue.dequeue(); log_mutex.unlock(); auto str = entryToString(qi); - log_ts << str << "\n"; - if (qi.cat == Level::Error) - piCerr << str; - else - piCout << str; + if (log_file.isOpened()) log_ts << str << "\n"; + if (output[Console]) { + PICout out(qi.cat == Level::Error ? piCerr : piCout); + if (color_console) { + switch (qi.cat) { + case Level::Error: out << PICoutManipulators::Red; break; + case Level::Warning: out << PICoutManipulators::Yellow; break; + default: break; + } + } + out << str; + } } } diff --git a/libs/main/application/pilog.h b/libs/main/application/pilog.h index 297fa37d..c13f260f 100644 --- a/libs/main/application/pilog.h +++ b/libs/main/application/pilog.h @@ -48,12 +48,27 @@ public: Debug, }; + enum Output { + File = 0x1, + Console = 0x2, + All = 0xFF, + }; + + //! \~english Set output target \"o\" to \"on\". + void setOutput(Output o, bool on = true) { output.setFlag(o, on); } + //! \~english Returns prefix for filename. PIString logName() const { return log_name; } //! \~english Set prefix for filename. Should be set \b before \a setDir()! void setLogName(const PIString & n) { log_name = n; } + //! \~english Returns if color for console output enabled. + bool colorConsole() const { return color_console; } + + //! \~english Set color for console output enabled. True by default. + void setColorConsole(bool yes) { color_console = yes; } + //! \~english Returns directory for log files. PIString dir() const { return log_dir; } @@ -89,10 +104,16 @@ public: //! \~english Set maximum level. All levels greater than \"l\" will be ignored. Default if \a Level::Debug. void setLevel(Level l); - + //! \~english Returns PICout for Level::Error level. PICout error(PIObject * context = nullptr); + + //! \~english Returns PICout for Level::Warning level. PICout warning(PIObject * context = nullptr); + + //! \~english Returns PICout for Level::Info level. PICout info(PIObject * context = nullptr); + + //! \~english Returns PICout for Level::Debug level. PICout debug(PIObject * context = nullptr); //! \~english Write all queued lines and stop. Also called in destructor. @@ -122,7 +143,9 @@ private: PIString log_dir, timestamp_format, line_format, line_format_p, log_name; PIQueue queue; PIMap id_by_cat; - Level max_level = Level::Debug; + Level max_level = Level::Debug; + PIFlags output = All; + bool color_console = true; int part_number = -1, cout_id = -1; }; diff --git a/libs/main/console/pikbdlistener.cpp b/libs/main/console/pikbdlistener.cpp index e959c52f..9e874a64 100644 --- a/libs/main/console/pikbdlistener.cpp +++ b/libs/main/console/pikbdlistener.cpp @@ -220,7 +220,6 @@ void PIKbdListener::readKeyboard() { #ifdef WINDOWS INPUT_RECORD ir; ReadConsoleInput(PRIVATE->hIn, &ir, 1, &(PRIVATE->ret)); - // piCout << ir.EventType; switch (ir.EventType) { case KEY_EVENT: { KEY_EVENT_RECORD ker = ir.Event.KeyEvent; @@ -542,6 +541,11 @@ void PIKbdListener::readKeyboard() { } +void PIKbdListener::stop() { + PIThread::stop(); +} + + void PIKbdListener::end() { // cout << "list end" << endl; #ifdef WINDOWS diff --git a/libs/main/console/pikbdlistener.h b/libs/main/console/pikbdlistener.h index 27080497..fb686304 100644 --- a/libs/main/console/pikbdlistener.h +++ b/libs/main/console/pikbdlistener.h @@ -180,6 +180,8 @@ public: void readKeyboard(); + void stop(); + //! Returns if keyboard listening is active (not running!) bool isActive() { return is_active; }