Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log with pantheios to a file?

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

like image 638
Vincent Avatar asked Nov 19 '10 13:11

Vincent


2 Answers

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

like image 193
dcw Avatar answered Sep 23 '22 01:09

dcw


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

like image 3
Tristan Piron Avatar answered Sep 24 '22 01:09

Tristan Piron