• Main Page
  • Files
  • File List
  • File Members

/home/bifh6/cs2009q3-i386/work/applauncherd-0.30.5+rq730927+0m6/src/invoker/report.c

Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (directui@nokia.com)
00006 **
00007 ** This file is part of applauncherd
00008 **
00009 ** If you have questions regarding the use of this file, please contact
00010 ** Nokia at directui@nokia.com.
00011 **
00012 ** This library is free software; you can redistribute it and/or
00013 ** modify it under the terms of the GNU Lesser General Public
00014 ** License version 2.1 as published by the Free Software Foundation
00015 ** and appearing in the file LICENSE.LGPL included in the packaging
00016 ** of this file.
00017 **
00018 ****************************************************************************/
00019 
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <stdarg.h>
00023 #include <syslog.h>
00024 
00025 #include "report.h"
00026 
00027 static enum report_output output = report_console;
00028 
00029 void report_set_output(enum report_output new_output)
00030 {
00031     if (output == new_output)
00032         return;
00033 
00034     if (output == report_syslog)
00035         closelog();
00036 
00037     if (new_output == report_syslog)
00038         openlog(PROG_NAME_INVOKER, LOG_PID, LOG_DAEMON);
00039 
00040     output = new_output;
00041 }
00042 
00043 static void vreport(enum report_type type, char *msg, va_list arg)
00044 {
00045     char str[400];
00046     char *str_type = "";
00047     int log_type;
00048 
00049     switch (type)
00050     {
00051     case report_debug:
00052         log_type = LOG_DEBUG;
00053         break;
00054     default:
00055     case report_info:
00056         log_type = LOG_INFO;
00057         break;
00058     case report_warning:
00059         str_type = "warning: ";
00060         log_type = LOG_WARNING;
00061         break;
00062     case report_error:
00063         str_type = "error: ";
00064         log_type = LOG_ERR;
00065         break;
00066     case report_fatal:
00067         str_type = "died: ";
00068         log_type = LOG_ERR;
00069         break;
00070     }
00071 
00072     vsnprintf(str, sizeof(str), msg, arg);
00073 
00074     // report errors and fatals to syslog even if it's not default output
00075     if ((output != report_syslog) &&
00076         ((type == report_error) || (type == report_fatal)))
00077     {
00078         enum report_output old_output = output;
00079         report_set_output(report_syslog);
00080         syslog(log_type, "%s%s", str_type, str);
00081         report_set_output(old_output);
00082     }
00083 
00084     if (output == report_console)
00085         printf("%s: %s%s", PROG_NAME_INVOKER, str_type, str);
00086     else if (output == report_syslog)
00087         syslog(log_type, "%s%s", str_type, str);
00088 }
00089 
00090 void report(enum report_type type, char *msg, ...)
00091 {
00092     va_list arg;
00093 
00094     va_start(arg, msg);
00095     vreport(type, msg, arg);
00096     va_end(arg);
00097 }
00098 
00099 void die(int status, char *msg, ...)
00100 {
00101     va_list arg;
00102 
00103     va_start(arg, msg);
00104     vreport(report_fatal, msg, arg);
00105     va_end(arg);
00106 
00107     exit(status);
00108 }
00109 

Generated on Mon Jul 4 2011 14:23:01 for applauncherd by  doxygen 1.7.1