How to log with pantheios to a file?
Asked Answered
W

4

5

I tried the exemple from pantheios to log to a file but can't manage to make it work. Messages are correctly displayed in the console but the log file isn't created. I tried to change severity levels since I saw that thread, but no one works.

Here's the code :

/* Pantheios Header Files */
#include <pantheios/pantheios.hpp>            // Pantheios C++ main header
#include <pantheios/inserters/args.hpp>       // for pantheios::args
#include <pantheios/inserters/exception.hpp>  // for pantheios::exception

#include <pantheios/backends/bec.file.h>      // be.file header

/* Standard C/C++ Header Files */
#include <exception>                          // for std::exception
#include <new>                                // for std::bad_alloc
#include <string>                             // for std::string
#include <stdlib.h>                           // for exit codes

/* ////////////////////////////////////////////////////////////////////// */

/* Define the stock front-end process identity, so that it links when using
* fe.N, fe.simple, etc. */
PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.cpp.file");

/* ////////////////////////////////////////////////////////////////////// */

#define PSTR(x)         PANTHEIOS_LITERAL_STRING(x)

/* ////////////////////////////////////////////////////////////////////// */

int main(int argc, char **argv)
{
    try
    {
#ifndef PANTHEIOS_USE_WIDE_STRINGS
        pantheios::log_DEBUG("main(", pantheios::args(argc, argv), ")");
#else /* ? !PANTHEIOS_USE_WIDE_STRINGS */
        STLSOFT_SUPPRESS_UNUSED(argc); STLSOFT_SUPPRESS_UNUSED(argv);
#endif /* !PANTHEIOS_USE_WIDE_STRINGS */

        pantheios::log_NOTICE(PSTR("stmt 1"));

        // Set the file name for the local back-end, truncating the
        // file's existing contents, if any.
        pantheios_be_file_setFilePath(PSTR("log.local"),     PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_LOCAL);

        pantheios::log_NOTICE(PSTR("stmt 2"));

        // Set the file name for the remote back-end.
        pantheios_be_file_setFilePath(PSTR("log.remote"), PANTHEIOS_BEID_REMOTE);

        pantheios::log_NOTICE(PSTR("stmt 3"));

        // Set the file name for all back-ends.
        pantheios_be_file_setFilePath(PSTR("log.all"));

    pantheios::log_NOTICE(PSTR("stmt 4"));

    pantheios::log_DEBUG(PSTR("exiting main()"));

    system("pause");
    return EXIT_SUCCESS;
}
catch(std::bad_alloc&)
{
    pantheios::log(pantheios::alert, PSTR("out of memory"));
}
catch(std::exception& x)
{
    pantheios::log_CRITICAL(PSTR("Exception: "), pantheios::exception(x));
}
catch(...)
{
    pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
}

return EXIT_FAILURE;
}

/* ///////////////////////////// end of file //////////////////////////// */

I have an "include_pantheios.cpp" file for implicit link purpose. Here it is :

/* Pantheios Header Files */
#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <platformstl/platformstl.h>
#include <pantheios/implicit_link/be.file.h>

#if (   defined(UNIX) || \
    defined(unix))&& \
    (   defined(_WIN32) || \
    defined(_WIN64))
# include <unixem/implicit_link.h>
#endif /* _WIN32 || _WIN64 */

Does somebody see where my problem come from? Thanks in advance,

Vincent

Whacking answered 19/11, 2010 at 13:35 Comment(0)
S
3

I think part of your confusion comes from the example doing too much: it shows local and remote files all in one. A simpler example would be:

// Headers for main()
#include <pantheios/pantheios.hpp> 
#include <pantheios/backends/bec.file.h> 

// Headers for implicit linking
#include <pantheios/implicit_link/core.h>
#include <pantheios/implicit_link/fe.simple.h>
#include <pantheios/implicit_link/be.file.h>

int main() {

  pantheios::log_NOTICE("log-1"); // save until log file set
  pantheios_be_file_setFilePath("mylogfile"); // sets log file; write "log-1" stmt
  pantheios::log_NOTICE("log-2"); // write "log-2" stmt
  pantheios_be_file_setFilePath(NULL); // close "mylogfile"


  pantheios::log_NOTICE("log-3"); // save until log file set
  pantheios_be_file_setFilePath("mylogfile2"); // sets log file; write "log-3" stmt
  pantheios::log_NOTICE("log-4"); // write "log-4" stmt
} // closes "mylogfile2" during program closedown

The problem with the original code, which I think comes from a Pantheios example program, is that it's trying to illustraet how to use local and remote back-ends at the same time as trying to illusteate how to use the be.file backend.

Forget all the different back-ends, and concentrate on the be.file-specific stuff.

HTH

Schleiermacher answered 20/11, 2010 at 11:1 Comment(2)
I tried your example but the result is the same. I can see the log in the console but the log file is never created. When I debugged, I found that the init was never called. I succeeded creating the log file and writing into it calling the pantheios_be_file_init and pantheios_be_file_logEntry functions. But like you said, those functions are not supposed to be implicitly called...Whacking
In contrast this example worked for me. I had to delete all the code I had an literally just use this example to get log files to start being produced. For me I had to call Init even tough I was working in C++. Here is a link to my post if your curious: #20998806Inculpate
E
3

I got the same problem, for future people, the problem is the order to link libraries

Pantheios Forum : https://sourceforge.net/projects/pantheios/forums/forum/475314/topic/5313841/index/page/1

I just link the pantheios.1.be.file.gcc44 before the pantheios.1.be.fprintf.gcc44

Earley answered 31/5, 2012 at 7:21 Comment(1)
+1 this worked great for me - I see it in /var/log/syslog and in stderr now, I need to write to /var/log/myapp/error.log - I wonder if I need my own backend (I'd like to avoid, if possible)...Neurosis
H
2

I think the issue is the order in which you link, but I don't quite see how it's possible given the code you posted.

I encountered the same issue, and I realized that it was because I was linking two backends at once: file and fprintf. More specifically, it was because I was linking fprintf before file. When I switched the order to link file first, then it would create and use the log file, but would not output to stdout when I commented out pantheios_be_file_setFilePath. So apparently whichever is linked first is the only one that will work (look up multiple backends).

Huonghupeh answered 22/5, 2011 at 3:6 Comment(0)
W
0

As far as I can tell this code is identical to the file stock back-end sample given with the library, so it ought to work.

How are you determining that the log files are not written? These are relative paths - try using absolute paths to be sure you are looking in the correct place.

If all else fails, you could debug through the code (after the filepath is set) to find out why nothing is getting written out.

Waltman answered 19/11, 2010 at 14:15 Comment(1)
Vincent: you don't call the back-end init function: that's done by the Pantheios runtime. When you link to be.file, whether explicit or implicit, then the init/uninit functions are called automatically. With most back-ends, you never even call a backend function. Only in the case of be.file do you do so, to set the log file path.Schleiermacher

© 2022 - 2024 — McMap. All rights reserved.