MIH_C_log.c 7.44 KB
Newer Older
hrizi's avatar
 
hrizi committed
1
/*******************************************************************************
2
    OpenAirInterface
hrizi's avatar
 
hrizi committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
    Copyright(c) 1999 - 2014 Eurecom

    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    OpenAirInterface 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
17 18
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
hrizi's avatar
 
hrizi committed
19 20 21 22 23 24
   see <http://www.gnu.org/licenses/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
25

hrizi's avatar
 
hrizi committed
26 27 28 29
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE

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

30 31
#define MIH_C_INTERFACE
#define MIH_C_LOG_C
32
/*
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
//-----------------------------------------------------------------------------
#include "MIH_C_log.h"
//-----------------------------------------------------------------------------
#define LOG_BUFF_SIZE  2048
static char        g_log_buffer_time[24];
static char        g_log_buffer[LOG_BUFF_SIZE];
static int         g_bypass_header;
//-----------------------------------------------------------------------------
char* getTimeStamp4Log()
//-----------------------------------------------------------------------------
{
    int len;
    struct timespec time_spec;
    unsigned int time_now_micros;
    unsigned int time_now_s;
    clock_gettime (CLOCK_REALTIME, &time_spec);
    time_now_s      = (unsigned int) time_spec.tv_sec % 3600;
    time_now_micros = (unsigned int) time_spec.tv_nsec/1000;
    //len=snprintf(g_log_buffer_time,24, "%06d:%06d", time_now_s, time_now_micros);
    len=snprintf(g_log_buffer_time,24, "%03d:%03d", time_now_s, time_now_micros);
   return g_log_buffer_time;
}
//-----------------------------------------------------------------------------
int is_newline( char *str, int size){
//-----------------------------------------------------------------------------
    int i;
    for (  i = 0; i < size; i++ ) {
      if ( str[i] == '\n' )
    return 1;
    }
63
    // if we get all the way to here, there must not have been a newline!
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    return 0;
}

// Initialize logging system
int MIH_C_log_init(unsigned int log_outputP) {
//-----------------------------------------------------------------------------
    g_mih_c_log_output = log_outputP;


    g_log_level2string[LOG_EMERG]         = "EMERG";
    g_log_level2string[LOG_ALERT]         = "ALERT";
    g_log_level2string[LOG_CRIT]          = "CRIT ";
    g_log_level2string[LOG_ERR]           = "ERR  ";
    g_log_level2string[LOG_WARNING]       = "WARN ";
    g_log_level2string[LOG_NOTICE]        = "NOTCE";
    g_log_level2string[LOG_INFO]          = "INFO ";
    g_log_level2string[LOG_DEBUG]         = "DEBUG";

    g_bypass_header = 0;

    switch (g_mih_c_log_output){
        case LOG_TO_CONSOLE:
            printf("***** mRAL V%s logging \n\n",MIH_C_VERSION);
            break;
        case LOG_TO_FILE:
            g_mih_c_log_file = fopen(MIH_C_LOGFILE_NAME,"w"); //start over new file
            if (g_mih_c_log_file == NULL){
                perror ("MIH_C_log_init - error opening file");
                exit(1);
            }
            fprintf(g_mih_c_log_file, "***** V%s starting logging \n\n",MIH_C_VERSION);
            fflush(g_mih_c_log_file);
            break;
        case LOG_TO_SYSTEM:
            openlog(MIH_C_SYSLOG_NAME, LOG_PID, LOG_LOCAL7);
            syslog(LOG_NOTICE,  "***** V%s starting logging \n\n",MIH_C_VERSION);
            break;
        default:
            printf("MIH_C_log_init: log_outputP error %d", log_outputP);
    }
    return 0;
}

//-----------------------------------------------------------------------------
// Log messages according to user settings
int MIH_C_log_record(int levelP, const char * log_msgP, ...) {
//-----------------------------------------------------------------------------
    struct timespec time_spec;
    unsigned int    time_now_micros;
    unsigned int    time_now_s;
    va_list         log_ap;
    int             len;


    switch (g_mih_c_log_output){
        case LOG_TO_CONSOLE:
            va_start(log_ap, log_msgP);
            len = vsnprintf(g_log_buffer, LOG_BUFF_SIZE-1, log_msgP, log_ap);
            va_end(log_ap);
            if (g_bypass_header == 0) {
                clock_gettime (CLOCK_REALTIME, &time_spec);
                time_now_s      = (unsigned int) time_spec.tv_sec;
                time_now_micros = (unsigned int) time_spec.tv_nsec/1000;
                printf("[%06d:%06d][%s][%s] ", time_now_s, time_now_micros, MIH_C_SYSLOG_NAME, g_log_level2string[levelP]);
           }
            printf("%s",g_log_buffer);
            fflush(stdout);
            fflush(stderr);
            if (is_newline(g_log_buffer,len) == 0 ){
                g_bypass_header = 1;
            } else {
                g_bypass_header = 0;
            }
            break;
        case LOG_TO_FILE:
            va_start(log_ap, log_msgP);
            len = vsnprintf(g_log_buffer, LOG_BUFF_SIZE-1, log_msgP, log_ap);
            va_end(log_ap);
            if (g_bypass_header == 0) {
                clock_gettime (CLOCK_REALTIME, &time_spec);
                time_now_s      = (unsigned int) time_spec.tv_sec;
                time_now_micros = (unsigned int) time_spec.tv_nsec/1000;
                fprintf(g_mih_c_log_file, "[%06d:%06d][%s][%s] ", time_now_s, time_now_micros, MIH_C_SYSLOG_NAME, g_log_level2string[levelP]);
            }
            fprintf(g_mih_c_log_file, "%s", g_log_buffer);
            fflush(g_mih_c_log_file);
            if (is_newline(g_log_buffer,len) == 0 ){
                g_bypass_header = 1;
            } else {
                g_bypass_header = 0;
            }
            break;
        case LOG_TO_SYSTEM:
            va_start(log_ap, log_msgP);
            syslog(levelP, log_msgP, log_ap);
            va_end(log_ap);
            return 0;
            break;
        default:
            printf("MIH_C_log_record: level error %d", levelP);
  }
  return 0;
}
//-----------------------------------------------------------------------------
// Close logging system
int MIH_C_log_exit(void) {
//-----------------------------------------------------------------------------
    switch (g_mih_c_log_output){
        case LOG_TO_CONSOLE:
            printf("***** stopping logging \n\n");
            break;
        case LOG_TO_FILE:
            fprintf(g_mih_c_log_file, "***** stopping logging \n\n");
            fflush(g_mih_c_log_file);
            fclose(g_mih_c_log_file);
            break;
        case LOG_TO_SYSTEM:
            syslog(LOG_NOTICE,  "***** stopping logging \n\n");
            closelog();
            break;
        default:
            printf("MIH_C_log_exit: output unrecognized error %d", g_mih_c_log_output);
    }
    return 0;
}
189
*/
190