Coder Social home page Coder Social logo

ezlog's Introduction

A tiny and flexible log library for C/C++. Written in C.

Platform tested: linux, win32, wince, mac

Usage:

####0. #include "ezlog.h" then Initialize ezlog. e.g.

ezlog_init_default();

This will display the message to console, and use the format "YY%-%MM%-%DD% %hh%:%mm%:%ss% {tid:%tid% pid:%pid}[%file%] %func% @%line%: %msg".

If you don't want to log to console, you can call ezlog_set_default_appender(0) after ezlog_init_default(), or just don't call ezlog_init_default() at all.

If you need to log to a file

ezlog_register_appender(file_appender("ezlog.txt", Append));

If you want to use other target, you can define your own appender.

typede (*appender)(consttruct {
    void (*handle)(const char* msg, void* opaque);
    void (*close)(void* opaque);
    void *opaque;
} appender_t;

e.g.

appender_t.handle is

void widget_appender_handle(const char* msg, void *opaque) {
    ((Widget*)opaque)->showText(msg);
}

appender_t.opaque is your widget instance

and for android

void android_appender_handle(const char* msg, void *) {
    __android_log_print(ANDROID_LOG_DEBUG, "%s", msg); //TODO: logtag
}

####1. Setup log message format (aka layout). ezlog supports global layout and appender specified layout.
If you use ezlog_init_default() before and wanna keep the defaul
format, skip this step. Log message will use the last format you defined.

Key words: YY, MM, DD, hh, mm, ss, ms, tid, pid, file, func, line, msg.
It's easy to understand what they mean, so I will not explain these. A key word must between two "%",
except the first and the last. If string between "%" is not a key word, ezlog will print it with out any change.
If use key word func, the complete function name including return type, class, parameters will be supported
if compiled with gcc.

set the global layout:

ezlog_set_global_layout("MM%-%DD% %hh%:%mm%:%ss% [tid:%tid%]-[%file%] %func% @%line%: %msg");

or the deprecated one

ezlog_init_layout("MM%-%DD% %hh%:%mm%:%ss% [tid:%tid%]-[%file%] %func% @%line%: %msg");

If you use ezlog_init_default(), skip this step.

Set an appender's layout:

void ezlog_set_appender_with_layout(appender handle, const char* format);

This function will add the appender if not exists.

####2. Start your logging. You can add additional messages. It supports printf like format. If nothing
else you want to put, just keep the parameter empty.

ezlog_debug("Hello, cruel world!");  
ezlog_warn("Damn! %s", __DATE__);  
ezlog_debug();                            //Only display the formated keywords message you defined  

####3. Clean up. If you use the gcc/clang toolchain (VC not supported yet), it will be called after main() auto.

ezlog_fini();

Qt Wrapper

Qt's logging functions such as qDebug(), qWarning() are designed customizable with your own message handler. This wrapper use ezlog as the handler.
It's very easy to use. just #include "qtezlog.h", and call

ezlog::QtEZLog::install();

Then qDebug(), qWarning() will be formated with ezlog's default layout.
TO restore the Qt's default, call

ezlog::QtEZLog::uninstall();

To use a new layout, call

ezlog::QtEZLog::setLayout(const QString& fmt);

[email protected]
20120224
Last updated: 20121202

ezlog's People

Contributors

wang-bin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ezlog's Issues

lock scope

now is global, can we use a smaller scope? mutex in struct? set null mutex in ezlog ?

vc6 support

it seems that vc6 does not support va args macro.

user defineded lock

like ffmpeg lockmgr. null means no lock, then user can use scoped lock outside instread of global lock.

remove ezmutex?

c++ macro to replace existing log system

define qDebug(...) ezlog_qt(FILE, ....).print()
what about qDebug() << xxx
class ezlog_qt {

}
operator <<

android need additional parameters in ctor
define android_log(...) ezlog_android(TAG, LEVEL, __FILE,....)
class ezlog_android {

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.