0

I just want to create a System logger using syslog.h in my application. I don't know how to start to create using that library and I downloaded the syslog in this link https://github.com/asankah/syslog-win32 but i don't know how it works. I searched related topics about on how to use syslog but i saw many topics on linux OS, I try it but it doesn't work. I targeted to create this syslog in windows OS and apply it in my application that I developed.

When I used this syntax:

#include "mainwindow.h"
#include <QApplication>
#include <syslog.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdarg.h>
#include <QString>
#include <QByteArray>
#include<QtCore/QtGlobal>
#include <QFile>
#include <QTextStream>
#pragma comment(lib,"Ws2_32.lib")
void SyslogMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    QByteArray localMsg = msg.toLocal8Bit();
    QString Stype;
    char fmt[] = {"Debug: %s", "Info: %s", "Warning: %s", "Critical: %s", "Fatal %s"};
       switch (type) {
       case QtDebugMsg:
           syslog(LOG_DEBUG, fmt[0], localMsg.constData());
           break;
       case QtInfoMsg:
           syslog(LOG_INFO, fmt[1], localMsg.constData());
           break;
       case QtWarningMsg:
           syslog(LOG_WARNING, fmt[2], localMsg.constData());
           break;
       case QtCriticalMsg:
           syslog(LOG_CRIT, fmt[3], localMsg.constData());
           break;
       case QtFatalMsg:
          syslog(LOG_ALERT, fmt[4], localMsg.constData());
          abort();
           break;
       }
       QFile outFile("Log.txt");
       outFile.open(QIODevice::WriteOnly | QIODevice::Append /*|QIODevice::Truncate*/);
       QTextStream ts(&outFile);
       QString log = QString("%1: %2 \n").arg(Stype).arg(msg).arg(context.file).arg(context.line).arg(context.function);
       QByteArray logMsg = log.toLocal8Bit();
       ts << logMsg << endl;
       outFile.close();

}

int main(int argc, char *argv[])
{
    init_syslog("localhost");
    qInstallMessageHandler(SyslogMessageHandler);
    QApplication a(argc, argv);
    MainWindow w;


    w.show();

    return a.exec();
}

The following error occurs:

void syslog(int,char *,...)': cannot convert argument 2 from const char [7]' to 'char *

error: LNK2019: unresolved external symbol init_syslog referenced in function main

I don't know what the problem is.

Eru El
  • 79
  • 1
  • 1
  • 8
  • Why do you want it yourself? Qt already has logging facilities that can be customized as per your needs. This might help you: https://stackoverflow.com/questions/4954140/how-to-redirect-qdebug-qwarning-qcritical-etc-output – Azeem Sep 20 '17 at 05:39
  • I updated my post. I need to know how to use that library to test if its much faster than in other qt facilities. because in the application that i developed, the debugging process going slow when I used the other qt logging facility. – Eru El Sep 21 '17 at 07:12
  • Can you be a bit more specific about the "facilities" that you use? And, what kind of slowness you are facing? Post your code also. – Azeem Sep 21 '17 at 08:05
  • The second argument for `void syslog( int pri, char* fmt, ... )' is `fmt` that needs to be the format just like `printf` function. You need to provide here `%s` i.e. `syslog(LOG_DEBUG, "Debug: %s", localMsg.constData());`. – Azeem Sep 21 '17 at 08:20

1 Answers1

0

The function expects char *, i.e. a modifiable string. You are providing an unmodifiable string literal (type const char[] or const char *).

This looks like a design flaw in the linked library. According to syslogc.c, the value is passed through functions syslog() and vsyslog() to the system function vsprintf_s() which correctly expects const char *format.

As a temporary solution, you could create a modifiable string and pass it to syslog():

char fmt[] = "Debug: %s"; // added %s to make it a usable format string
syslog(LOG_DEBUG, fmt, localMsg.constData());

A permanent solution would be forking the library, correcting the parameter types and optionally creating a pull request.

Melebius
  • 6,183
  • 4
  • 39
  • 52
  • It's `pull request`, actually. – Azeem Sep 21 '17 at 10:01
  • I try the temporary solution: `char fmt[] = {"Debug: %s", "Info: %s", "Warning: %s", "Critical: %s", "Fatal %s"}; syslog(LOG_DEBUG, fmt[0], localMsg.constData());` but the result is like this: `void syslog(int,char *,...)': cannot convert argument 2 from 'char' to 'char *'` Is the init_syslog() can be call in the main function just like qInstallMessageHandler()? And I don't know how to fork the existing library in my project. Any thoughts? – Eru El Sep 22 '17 at 01:57
  • @EruEl The _char_ array type (`char fmt[]`) I suggested is not the same as your _string_ array. A string array is a 2D char array, so use `char fmt[][]`. – Melebius Sep 22 '17 at 05:25
  • but i changed it, and the same error happens. also when i use the `init_syslog("localhost")`, this error message happen. `unresolved external symbol init_syslog referenced in function main` what will be the problem? – Eru El Sep 22 '17 at 06:08
  • @EruEl Exactly the same error, letter after letter? And for what code? I noticed you had updated it in the question for the previous variant but have not corrected the string array problem yet. The “unresolved external symbol” is a quite different problem and Stack Overflow has [a plenty of these questions](https://stackoverflow.com/search?q=unresolved+external+symbol) already. Please search them first and do not mix two problems in one question. – Melebius Sep 26 '17 at 08:09