Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

cpp/example.cpp.custom_fe/example.cpp.custom_fe.cpp

Demonstrates how to implement a custom front-end that operates with a severity filtering level set at runtime.

/* /////////////////////////////////////////////////////////////////////////
 * File:        examples/cpp/example.cpp.custom_fe/example.cpp.custom_fe.cpp
 *
 * Purpose:     C++ example program for Pantheios. Demonstrates:
 *
 *                - definition of a custom front-end that supports tabbed output
 *                - use of pantheios::logputs() in bail-out conditions
 *
 * Created:     31st August 2006
 * Updated:     21st October 2008
 *
 * www:         http://www.pantheios.org/
 *
 * License:     This source code is placed into the public domain 2006
 *              by Synesis Software Pty Ltd. There are no restrictions
 *              whatsoever to your use of the software.
 *
 *              This software is provided "as is", and any warranties,
 *              express or implied, of any kind and for any purpose, are
 *              disclaimed.
 *
 * ////////////////////////////////////////////////////////////////////// */


/* Pantheios Header Files */
#include <pantheios/pantheios.hpp>              // Pantheios C++ main header
#include <pantheios/frontend.h>

/* 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, atoi()

#ifndef PANTHEIOS_DOCUMENTATION_SKIP_SECTION
# if defined(STLSOFT_COMPILER_IS_MSVC)
#  pragma warning(disable : 4702)
# endif /* compiler */
#endif /* !PANTHEIOS_DOCUMENTATION_SKIP_SECTION */

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

namespace
{
  // By default, we will log everything at NOTICE and below (remember that
  // they get more serious as the values get lower).

  static int  s_severityCeiling = pantheios::notice;

} // anonymous namespace

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

// USAGE: [<severity-ceiling>]
//
// where:
// <severity-ceiling> - a number between 0 and 7, which sets the maximum level
//                      displayed, or -1 to suppress all output.

int main(int argc, char **argv)
{
  if(argc > 1)
  {
    s_severityCeiling = ::atoi(argv[1]);
  }

  try
  {
    pantheios::log_DEBUG("debug statement");
    pantheios::log_INFORMATIONAL("informational statement");
    pantheios::log_NOTICE("notice statement");
    pantheios::log_WARNING("warning statement");
    pantheios::log_ERROR("error statement");
    pantheios::log_CRITICAL("critical statement");
    pantheios::log_ALERT("alert statement");
    pantheios::log_EMERGENCY("emergency statement");


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

  return EXIT_FAILURE;
}

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

PANTHEIOS_CALL(int) pantheios_fe_init(  void*   /* reserved */
                                    ,   void**  ptoken)
{
  *ptoken = NULL;

  return 0;
}

PANTHEIOS_CALL(void) pantheios_fe_uninit(void* /* token */)
{}

PANTHEIOS_CALL(char const*) pantheios_fe_getProcessIdentity(void* /* token */)
{
  return "example.cpp.custom_fe";
}

PANTHEIOS_CALL(int) pantheios_fe_isSeverityLogged(  void*   /* token */
                                                ,   int     severity
                                                ,   int     /* backEndId */)
{
  return severity <= s_severityCeiling;
}

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

pantheios Library documentation © Matthew Wilson & Synesis Software, 2006-2009 SourceForge.net Logo