PILog ready to use

This commit is contained in:
2024-09-19 17:26:58 +03:00
parent aa963a4bda
commit 4acab04895
4 changed files with 53 additions and 14 deletions

View File

@@ -92,8 +92,10 @@ PILog::~PILog() {
void PILog::setDir(const PIString & d) { void PILog::setDir(const PIString & d) {
stopAndWait(); stopAndWait();
log_dir = d; log_dir = d;
PIDir::make(log_dir); if (output[File]) {
newFile(); PIDir::make(log_dir);
newFile();
}
start(); start();
} }
@@ -164,7 +166,6 @@ PICout PILog::makePICout(PIObject * context, Level cat) {
void PILog::enqueue(const PIString & msg, Level cat) { void PILog::enqueue(const PIString & msg, Level cat) {
if (log_file.isClosed()) return;
auto t = PIDateTime::fromSystemTime(PISystemTime::current()); auto t = PIDateTime::fromSystemTime(PISystemTime::current());
PIMutexLocker ml(log_mutex); PIMutexLocker ml(log_mutex);
queue.enqueue({cat, t, msg}); queue.enqueue({cat, t, msg});
@@ -190,9 +191,11 @@ void PILog::newFile() {
void PILog::run() { void PILog::run() {
if (split_tm.elapsed() >= split_time) { if (output[File]) {
split_tm.reset(); if (split_tm.elapsed() >= split_time) {
newFile(); split_tm.reset();
newFile();
}
} }
log_mutex.lock(); log_mutex.lock();
if (queue.isEmpty()) { if (queue.isEmpty()) {
@@ -210,10 +213,17 @@ void PILog::run() {
auto qi = queue.dequeue(); auto qi = queue.dequeue();
log_mutex.unlock(); log_mutex.unlock();
auto str = entryToString(qi); auto str = entryToString(qi);
log_ts << str << "\n"; if (log_file.isOpened()) log_ts << str << "\n";
if (qi.cat == Level::Error) if (output[Console]) {
piCerr << str; PICout out(qi.cat == Level::Error ? piCerr : piCout);
else if (color_console) {
piCout << str; switch (qi.cat) {
case Level::Error: out << PICoutManipulators::Red; break;
case Level::Warning: out << PICoutManipulators::Yellow; break;
default: break;
}
}
out << str;
}
} }
} }

View File

@@ -48,12 +48,27 @@ public:
Debug, 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. //! \~english Returns prefix for filename.
PIString logName() const { return log_name; } PIString logName() const { return log_name; }
//! \~english Set prefix for filename. Should be set \b before \a setDir()! //! \~english Set prefix for filename. Should be set \b before \a setDir()!
void setLogName(const PIString & n) { log_name = n; } 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. //! \~english Returns directory for log files.
PIString dir() const { return log_dir; } 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. //! \~english Set maximum level. All levels greater than \"l\" will be ignored. Default if \a Level::Debug.
void setLevel(Level l); void setLevel(Level l);
//! \~english Returns PICout for Level::Error level.
PICout error(PIObject * context = nullptr); PICout error(PIObject * context = nullptr);
//! \~english Returns PICout for Level::Warning level.
PICout warning(PIObject * context = nullptr); PICout warning(PIObject * context = nullptr);
//! \~english Returns PICout for Level::Info level.
PICout info(PIObject * context = nullptr); PICout info(PIObject * context = nullptr);
//! \~english Returns PICout for Level::Debug level.
PICout debug(PIObject * context = nullptr); PICout debug(PIObject * context = nullptr);
//! \~english Write all queued lines and stop. Also called in destructor. //! \~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; PIString log_dir, timestamp_format, line_format, line_format_p, log_name;
PIQueue<Entry> queue; PIQueue<Entry> queue;
PIMap<Level, int> id_by_cat; PIMap<Level, int> id_by_cat;
Level max_level = Level::Debug; Level max_level = Level::Debug;
PIFlags<Output> output = All;
bool color_console = true;
int part_number = -1, cout_id = -1; int part_number = -1, cout_id = -1;
}; };

View File

@@ -220,7 +220,6 @@ void PIKbdListener::readKeyboard() {
#ifdef WINDOWS #ifdef WINDOWS
INPUT_RECORD ir; INPUT_RECORD ir;
ReadConsoleInput(PRIVATE->hIn, &ir, 1, &(PRIVATE->ret)); ReadConsoleInput(PRIVATE->hIn, &ir, 1, &(PRIVATE->ret));
// piCout << ir.EventType;
switch (ir.EventType) { switch (ir.EventType) {
case KEY_EVENT: { case KEY_EVENT: {
KEY_EVENT_RECORD ker = ir.Event.KeyEvent; KEY_EVENT_RECORD ker = ir.Event.KeyEvent;
@@ -542,6 +541,11 @@ void PIKbdListener::readKeyboard() {
} }
void PIKbdListener::stop() {
PIThread::stop();
}
void PIKbdListener::end() { void PIKbdListener::end() {
// cout << "list end" << endl; // cout << "list end" << endl;
#ifdef WINDOWS #ifdef WINDOWS

View File

@@ -180,6 +180,8 @@ public:
void readKeyboard(); void readKeyboard();
void stop();
//! Returns if keyboard listening is active (not running!) //! Returns if keyboard listening is active (not running!)
bool isActive() { return is_active; } bool isActive() { return is_active; }