Skip to content

robert1207/MyLog

Repository files navigation

MyLog is a log-lib for Qt cross-platform applications. MyLog is stable and easy to use.

Read this in other languages: English, 简体中文

Features

  1. Colorful text console output ;
  2. Support log levels of "info debug error";
  3. Multi-thread safe;
  4. Multi-threaded calls will not cause infinite memory growth;
  5. Support write log into a file, console and self-made-logger;
  6. Support self-implement logger;
  7. MyLog is an open-source project;
  8. Using MyLog is as easy as "qDebug()" and as powerfull as "qDebug()"(Cause MyLog is base on QDebug class);
  9. Could log(output) extra details, such as "level", "timestamp", "code file name" , "function name", "line number";

Usage

you should init MyLog before you log-out any string. e.g. Init the MyLog at the main function. Note: Don't start a thread which using "MyLog" to log-out at main function directly

1.Include the lib or src at your xxx.pro

#using MyLog as the src (using your own path)
#include($$PWD/my_lib/MyLog/MyLogSrc.pri)

#using MyLog as a lib (using your own path)
include($$PWD/my_lib/MyLog/MyLogLib.pri)
  1. Include header
#include "my_log.h"
  1. Init for file log
QCoreApplication::setApplicationName("myappname");
QCoreApplication::setApplicationVersion("0.0.1");
QCoreApplication::setOrganizationName("com.company.myappname"); //set app name for log-file-path
QCoreApplication::setOrganizationDomain("com.company.myappname");

MyLogNS::FileLogger *fileLog = new MyLogNS::FileLogger();
int result = fileLog->open_log_file_at_dir("log");
if(result != 0) {
    qDebug("error: %s", fileLog->get_error_str(result));
}
qDebug("log file path=%s", fileLog->get_log_file_abs_path());
MyLogIns.installer_logger(fileLog);
  1. Init for console log
MyLogIns.installer_logger(new MyLogNS::ConsoleLogger());
  1. Features control variables using the variables when you need change the features only.
MyLogIns.is_enable_auto_new_line= true;     //default is true
MyLogIns.is_show_level_str= true;           //default is true
MyLogIns.is_show_timestamp= true;           //default is true
MyLogIns.is_show_file_name= true;           //default is false
MyLogIns.is_show_function_name= true;       //default is true
MyLogIns.is_show_line_number= true;         //default is true
  1. Write log (use it as the way using the "qDebug()")
I << "str value=" << 1;     //log info
D << "str value=" << 2;     //log debug
E << "str value=" << 3;     //log error
D << "Date:" << QDate::currentDate();
D << "Types:" << QString("String") << QChar('x') << QRect(0, 10, 50, 40);
  1. Switch about each level of log you can find the macro at “MyLogSrc.pri” or “MyLogLib.pri”
#DEFINES += MYLOG_NO_I_OUTPUT
#DEFINES += MYLOG_NO_D_OUTPUT
#DEFINES += MYLOG_NO_E_OUTPUT
  1. Log example on console

out-put-example

Making your own Logger

  1. Method of making a self-implemented logger inherit this interface and make your own logger. you can find this inferface at file "logger_interface.h"
class LoggerInterface
{
public:
    LoggerInterface();
    virtual ~LoggerInterface();
public:
    virtual void open() = 0;
    virtual void close() = 0;
    virtual void write(LogLevel level, const QString &msg, bool is_shift_to_next_line) = 0;
};
  1. Create your own logger
#include "logger_interface.h"
class NetLogger : public MyLogNS::LoggerInterface
{
public:
    NetLogger();
    virtual void open();
    virtual void close();
    virtual void write(MyLogNS::LogLevel level, const QString &msg, bool is_shift_to_next_line);
};
  1. Using your own logger
MyLogIns.installer_logger(new  NetLogger());

About

MyLog is a log-lib for Qt applications.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published