/*! \file pilog.h * \ingroup Application * \~\brief * \~english High-level log * \~russian Высокоуровневый лог */ /* PIP - Platform Independent Primitives High-level log Ivan Pelipenko peri4ko@yandex.ru This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #ifndef PIlog_H #define PIlog_H #include "pifile.h" #include "piiostream.h" #include "pithread.h" //! \ingroup Application //! \~\brief //! \~english High-level log //! \~russian Высокоуровневый лог class PIP_EXPORT PILog: public PIThread { PIOBJECT_SUBCLASS(PILog, PIThread) public: PILog(); ~PILog(); //! \~english Returns prefix for filename. PIString applicationName() const { return app_name; } //! \~english Set prefix for filename. Should be set \b before \a setDir()! void setApplicationName(const PIString & n) { app_name = n; } //! \~english Returns directory for log files. PIString dir() const { return log_dir; } //! \~english Set directory for log files. Should be set \b after \a setApplicationName()! void setDir(const PIString & d); //! \~english Returns lifetime for file. PISystemTime fileSplitTime() const { return split_time; } //! \~english Set lifetime for file. Each "st" interval new file will be created. void setFileSplitTime(PISystemTime st) { split_time = st; } //! \~english Returns timestamp format for line. PIString timestampFormat() const { return timestamp_format; } //! \~english Set timestamp format for line. Default is "yyyy-MM-dd hh:mm:ss.zzz". void setTimestampFormat(const PIString & f) { timestamp_format = f; } //! \~english Returns line format. PIString lineFormat() const { return line_format; } //! \~english Set line format. "t" is timestamp, "c" is category and "m" is message. Default is "t - c: m". void setLineFormat(const PIString & f); PICout debug(PIObject * context = nullptr); PICout warning(PIObject * context = nullptr); PICout error(PIObject * context = nullptr); //! \~english Write all queued lines and stop. Also called in destructor. void stop(); private: enum class Category { Debug, Warning, Error }; EVENT_HANDLER2(void, coutDone, int, id, PIString *, buff); PICout makePICout(PIObject * context, Category cat); void enqueue(const PIString & msg, Category cat = Category::Debug); struct Entry { Category cat; PIDateTime time; PIString msg; }; PIString entryToString(const Entry & e) const; void newFile(); void run() override; PIMutex log_mutex, cout_mutex; PIFile log_file; PIIOTextStream log_ts; PITimeMeasurer split_tm; PISystemTime split_time; PIString log_dir, timestamp_format, line_format, line_format_p, app_name; PIQueue queue; PIMap cout_cat_by_id; int part_number = -1, cout_id = -1; }; #endif