C 中的日志错误

发布于 2024-12-17 22:14:34 字数 238 浏览 3 评论 0原文

我正在 Linux 环境下开发一个 C 项目,我正在寻找一种向日志文件添加错误的有效方法。 我尝试使用 Syslog 进行以下初始化:

setlogmask(LOG_UPTO(7));
openlog(name, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER);

但似乎它运行得太慢。我需要它工作得非常快.. 有人可以帮忙吗?也许系统日志不是正确的方法。

I am working on a project in C under a Linux environment and I'm looking for an efficient way to add errors to a log file.
I tried to use Syslog with the following initialization:

setlogmask(LOG_UPTO(7));
openlog(name, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER);

But it seems that it works too slow. I need it to work very fast..
Can someone help with that? Maybe the syslog is not the right approach.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何其悲哀 2024-12-24 22:14:35

一种可能会失去使用 syslog 的一些灵活性,即让程序使用普通 I/O 设施(可能要小心使用刷新)自行写入错误日志。

One possibility, which loses some of the flexibility of using syslog, is to have your program write its error log itself, using the normal I/O facilities (probably with careful use of flushing).

江南烟雨〆相思醉 2024-12-24 22:14:35

您可以编写一个自定义的轻量级记录器,也可以使用第 3 方开源记录器...

例如,第 3 部分 C++ 记录器 [ http://logging.apache.org/log4cxx/]

这里是简单的 [buggy] 自定义记录器 [ 摘自《C++ Time saving Techniques For Dummies》一书]

#ifndef __SimpleLogger__
#define __SimpleLogger__


#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdarg.h>
#include <fstream>
using namespace std;


class SimpleLogger
{


public:
    SimpleLogger( void )
    {
        bOn = false;
        bForceFlush = false;
    }
    SimpleLogger( const char *strFileName )

    {
        sFileName = strFileName;
        bOn = false;
        bForceFlush = false;
    }
    SimpleLogger( const SimpleLogger& aCopy )
    {
        sFileName = aCopy.sFileName;
        bForceFlush = aCopy.bForceFlush;
        setOn( aCopy.bOn );
    }
    virtual ~SimpleLogger()
    {
        Flush();
        if ( bOn )
            file.close();
    }
    void setOn( bool flag )
    {
        bOn = flag;
        if ( bOn )
        {
            file.open( sFileName.c_str() ,fstream::in | fstream::out | fstream::app);
        }
    }
    bool getOn( void )
    {
        return bOn;
    }
    void setForceFlush( bool flag )
    {
        bForceFlush = flag;
    }
    bool getForceFlush( void )
    {
        return bForceFlush;
    }
    void setFileName ( const char
        *strFileName )
    {
        sFileName = strFileName;
    }
    string getFileName ( void )
    {
        return sFileName;
    }
    void Log( const char *strMessage )
    {
        sMessage += strMessage;
        sMessage += "\n";
        if ( bForceFlush )
            Flush();
    }
    void LogString( const char *fmt, ... )

    {
        char szBuffer[256];
        va_list marker;
        va_start( marker, fmt );     
        vsprintf(szBuffer, fmt, marker );
        sMessage += szBuffer;
        sMessage += "\n";
        if ( bForceFlush )
            Flush();

    }
    void Flush( void )
    {
        if ( bOn )
            file << sMessage << endl;
        sMessage = "";
    }


private:
    bool   bOn;
    bool   bForceFlush;
    string   sMessage;
    string   sFileName;
    ofstream  file;

};


#endif

用法:

 SimpleLogger logger("MyLog.txt");
 logger.setForceFlush( true );
 logger.setOn(true);

 logger.Log("I am the buggy logger");

警告:这是一个“玩具”记录器,有一些错误,仅供您了解一些自定义记录器...不要直接在实际应用程序中使用..

You can write a custom light weight logger or may 3rd party open source one...

For example 3rd part C++ logger [ http://logging.apache.org/log4cxx/]

And Here is simple [buggy] custom logger [ From book C++ Timesaving Techniques For Dummies ]

#ifndef __SimpleLogger__
#define __SimpleLogger__


#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdarg.h>
#include <fstream>
using namespace std;


class SimpleLogger
{


public:
    SimpleLogger( void )
    {
        bOn = false;
        bForceFlush = false;
    }
    SimpleLogger( const char *strFileName )

    {
        sFileName = strFileName;
        bOn = false;
        bForceFlush = false;
    }
    SimpleLogger( const SimpleLogger& aCopy )
    {
        sFileName = aCopy.sFileName;
        bForceFlush = aCopy.bForceFlush;
        setOn( aCopy.bOn );
    }
    virtual ~SimpleLogger()
    {
        Flush();
        if ( bOn )
            file.close();
    }
    void setOn( bool flag )
    {
        bOn = flag;
        if ( bOn )
        {
            file.open( sFileName.c_str() ,fstream::in | fstream::out | fstream::app);
        }
    }
    bool getOn( void )
    {
        return bOn;
    }
    void setForceFlush( bool flag )
    {
        bForceFlush = flag;
    }
    bool getForceFlush( void )
    {
        return bForceFlush;
    }
    void setFileName ( const char
        *strFileName )
    {
        sFileName = strFileName;
    }
    string getFileName ( void )
    {
        return sFileName;
    }
    void Log( const char *strMessage )
    {
        sMessage += strMessage;
        sMessage += "\n";
        if ( bForceFlush )
            Flush();
    }
    void LogString( const char *fmt, ... )

    {
        char szBuffer[256];
        va_list marker;
        va_start( marker, fmt );     
        vsprintf(szBuffer, fmt, marker );
        sMessage += szBuffer;
        sMessage += "\n";
        if ( bForceFlush )
            Flush();

    }
    void Flush( void )
    {
        if ( bOn )
            file << sMessage << endl;
        sMessage = "";
    }


private:
    bool   bOn;
    bool   bForceFlush;
    string   sMessage;
    string   sFileName;
    ofstream  file;

};


#endif

Usage :

 SimpleLogger logger("MyLog.txt");
 logger.setForceFlush( true );
 logger.setOn(true);

 logger.Log("I am the buggy logger");

Warning: This is "toy" logger with some bugs to just give you some idea about custom logger...Do not use directly in real applications..

撩人痒 2024-12-24 22:14:35

也许您的系统日志守护进程在每次写入后都会进行同步。另一种方法可能是按照其他人的建议直接记录到文件中。

Probably your syslog daemon is doing a sync after each write. An alternative might be to log directly to a file as suggested by others.

赏烟花じ飞满天 2024-12-24 22:14:35

使用 printk(KERNEL "...") 并使用 dmesg 获取日志怎么样?

What about using printk(KERNEL "..."), and get your logs with dmesg ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文