trema/trema-edge

View on GitHub
ruby/trema/logger.c

Summary

Maintainability
Test Coverage
/*
 * Copyright (C) 2008-2013 NEC Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include "checks.h"
#include "log.h"
#include "logger.h"


extern VALUE mTrema;
VALUE mLogger;


static VALUE
do_log( void ( *log_function )( const char *format, ... ), int argc, VALUE *argv ) {
  VALUE message = rb_f_sprintf( argc, argv );
  log_function( StringValuePtr( message ) );
  return message;
}


/*
 * @overload critical(format ...)
 *   Outputs a message representing that "the system is completely
 *   unusable" to log file.
 *
 *   @example
 *     critical "Trema blue screen. Memory dump = %s", memory
 *
 *   @return [String] the string resulting from applying format to any
 *     additional arguments.
 */
static VALUE
logger_critical( int argc, VALUE *argv, VALUE self ) {
  UNUSED( self );
  return( do_log( critical, argc, argv ) );
}


/*
 * @overload error(format ...)
 *   Outputs a message representing that "something went wrong" to log
 *   file.
 *
 *   @example
 *     error "Failed to accept %s", app_socket
 *
 *   @return [String] the string resulting from applying format to any
 *     additional arguments.
 */
static VALUE
logger_error( int argc, VALUE *argv, VALUE self ) {
  UNUSED( self );
  return( do_log( error, argc, argv ) );
}


/*
 * @overload warn(format ...)
 *   Outputs a message representing that "something in the system was
 *   not as expected" to log file.
 *
 *   @example
 *     warn "%s: trema is already initialized", app_name
 *
 *   @return [String] the string resulting from applying format to any
 *     additional arguments.
 */
static VALUE
logger_warn( int argc, VALUE *argv, VALUE self ) {
  UNUSED( self );
  return( do_log( warn, argc, argv ) );
}


/*
 * @overload notice(format ...)
 *   Outputs a message representing that "normal but significant
 *   condition occurred" to log file.
 *
 *   @example
 *     notice "The switch %s disconnected its secure channel connection", datapath_id
 *
 *   @return [String] the string resulting from applying format to any
 *     additional arguments.
 */
static VALUE
logger_notice( int argc, VALUE *argv, VALUE self ) {
  UNUSED( self );
  return( do_log( notice, argc, argv ) );
}


/*
 * @overload info(format ...)
 *   Outputs an informational massage to log file.
 *
 *   @example
 *     info "Hello world from %s!", datapath_id
 *
 *   @return [String] the string resulting from applying format to any
 *     additional arguments.
 */
static VALUE
logger_info( int argc, VALUE *argv, VALUE self ) {
  UNUSED( self );
  return( do_log( info, argc, argv ) );
}


/*
 * @overload debug(format ...)
 *   Outputs a debug-level massage to log file.
 *
 *   @example
 *     debug "Setting a packet_in handler: %s", method
 *
 *   @return [String] the string resulting from applying format to any
 *     additional arguments.
 */
static VALUE
logger_debug( int argc, VALUE *argv, VALUE self ) {
  UNUSED( self );
  return( do_log( debug, argc, argv ) );
}


void
Init_logger( void ) {
  mTrema = rb_define_module( "Trema" );
  mLogger = rb_define_module_under( mTrema, "Logger" );

  rb_define_method( mLogger, "critical", logger_critical, -1 );
  rb_define_method( mLogger, "error", logger_error, -1 );
  rb_define_method( mLogger, "warn", logger_warn, -1 );
  rb_define_method( mLogger, "notice", logger_notice, -1 );
  rb_define_method( mLogger, "info", logger_info, -1 );
  rb_define_method( mLogger, "debug", logger_debug, -1 );
}


/*
 * Local variables:
 * c-basic-offset: 2
 * indent-tabs-mode: nil
 * End:
 */