log.c 21.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file log.c
* \brief log implementaion
* \author Navid Nikaein
* \date 2009 - 2014
* \version 0.5
* @ingroup util

*/

#define _GNU_SOURCE  /* required for pthread_getname_np */
//#define LOG_TEST 1

#define COMPONENT_LOG
#define COMPONENT_LOG_IF
#include <ctype.h>
#define LOG_MAIN
#include "log.h"
#include "vcd_signal_dumper.h"
#include "assertions.h"

42 43 44
#include <pthread.h>
#include <string.h>
#include <linux/prctl.h>
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include "common/config/config_userapi.h"
// main log variables




mapping log_level_names[] = {
  {"error",  OAILOG_ERR},
  {"warn",   OAILOG_WARNING},
  {"info",   OAILOG_INFO},
  {"debug",  OAILOG_DEBUG},
  {"trace",  OAILOG_TRACE},
  {NULL, -1}
};

mapping log_options[] = {
  {"nocolor", FLAG_NOCOLOR  },
  {"level",   FLAG_LEVEL  },
  {"thread",  FLAG_THREAD },
  {NULL,-1}
};


68
mapping log_maskmap[] = LOG_MASKMAP_INIT;
69

70 71
char *log_level_highlight_start[] = {LOG_RED, LOG_ORANGE, "", LOG_BLUE, LOG_CYBL};  /*!< \brief Optional start-format strings for highlighting */
char *log_level_highlight_end[]   = {LOG_RESET,LOG_RESET,LOG_RESET, LOG_RESET,LOG_RESET};   /*!< \brief Optional end-format strings for highlighting */
72 73


74
int write_file_matlab(const char *fname,const char *vname,void *data,int length,int dec,char format) {
75 76 77 78
  FILE *fp=NULL;
  int i;

  if (data == NULL)
79
    return -1;
80

81
  //printf("Writing %d elements of type %d to %s\n",length,format,fname);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

  if (format == 10 || format ==11 || format == 12 || format == 13 || format == 14) {
    fp = fopen(fname,"a+");
  } else if (format != 10 && format !=11  && format != 12 && format != 13 && format != 14) {
    fp = fopen(fname,"w+");
  }

  if (fp== NULL) {
    printf("[OPENAIR][FILE OUTPUT] Cannot open file %s\n",fname);
    return(-1);
  }

  if (format != 10 && format !=11  && format != 12 && format != 13 && format != 14)
    fprintf(fp,"%s = [",vname);

  switch (format) {
98 99 100 101
    case 0:   // real 16-bit
      for (i=0; i<length; i+=dec) {
        fprintf(fp,"%d\n",((short *)data)[i]);
      }
102

103
      break;
104

105 106 107 108 109 110 111
    case 1:  // complex 16-bit
    case 13:
    case 14:
    case 15:
      for (i=0; i<length<<1; i+=(2*dec)) {
        fprintf(fp,"%d + j*(%d)\n",((short *)data)[i],((short *)data)[i+1]);
      }
112

113
      break;
114

115 116 117 118
    case 2:  // real 32-bit
      for (i=0; i<length; i+=dec) {
        fprintf(fp,"%d\n",((int *)data)[i]);
      }
119

120
      break;
121

122 123 124 125
    case 3: // complex 32-bit
      for (i=0; i<length<<1; i+=(2*dec)) {
        fprintf(fp,"%d + j*(%d)\n",((int *)data)[i],((int *)data)[i+1]);
      }
126

127
      break;
128

129 130 131 132
    case 4: // real 8-bit
      for (i=0; i<length; i+=dec) {
        fprintf(fp,"%d\n",((char *)data)[i]);
      }
133

134
      break;
135

136 137 138 139
    case 5: // complex 8-bit
      for (i=0; i<length<<1; i+=(2*dec)) {
        fprintf(fp,"%d + j*(%d)\n",((char *)data)[i],((char *)data)[i+1]);
      }
140

141
      break;
142

143 144 145 146
    case 6:  // real 64-bit
      for (i=0; i<length; i+=dec) {
        fprintf(fp,"%lld\n",((long long *)data)[i]);
      }
147

148
      break;
149

150 151 152 153
    case 7: // real double
      for (i=0; i<length; i+=dec) {
        fprintf(fp,"%g\n",((double *)data)[i]);
      }
154

155
      break;
156

157 158 159 160
    case 8: // complex double
      for (i=0; i<length<<1; i+=2*dec) {
        fprintf(fp,"%g + j*(%g)\n",((double *)data)[i], ((double *)data)[i+1]);
      }
161

162
      break;
163

164 165 166 167
    case 9: // real unsigned 8-bit
      for (i=0; i<length; i+=dec) {
        fprintf(fp,"%d\n",((unsigned char *)data)[i]);
      }
168

169
      break;
170

171 172 173 174 175 176 177 178 179
    case 10 : // case eren 16 bit complex :
      for (i=0; i<length<<1; i+=(2*dec)) {
        if((i < 2*(length-1)) && (i > 0))
          fprintf(fp,"%d + j*(%d),",((short *)data)[i],((short *)data)[i+1]);
        else if (i == 2*(length-1))
          fprintf(fp,"%d + j*(%d);",((short *)data)[i],((short *)data)[i+1]);
        else if (i == 0)
          fprintf(fp,"\n%d + j*(%d),",((short *)data)[i],((short *)data)[i+1]);
      }
180

181
      break;
182

183 184 185 186 187 188 189 190 191
    case 11 : //case eren 16 bit real for channel magnitudes:
      for (i=0; i<length; i+=dec) {
        if((i <(length-1))&& (i > 0))
          fprintf(fp,"%d,",((short *)data)[i]);
        else if (i == (length-1))
          fprintf(fp,"%d;",((short *)data)[i]);
        else if (i == 0)
          fprintf(fp,"\n%d,",((short *)data)[i]);
      }
192

193 194
      printf("\n eren: length :%d",length);
      break;
195

196 197 198
    case 12 : // case eren for log2_maxh real unsigned 8 bit
      fprintf(fp,"%d \n",((unsigned char *)&data)[0]);
      break;
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  }

  if (format != 10 && format !=11 && format !=12 && format != 13 && format != 15) {
    fprintf(fp,"];\n");
    fclose(fp);
    return(0);
  } else if (format == 10 || format ==11 || format == 12 || format == 13 || format == 15) {
    fclose(fp);
    return(0);
  }

  return 0;
}

/* get log parameters from configuration file */
void  log_getconfig(log_t *g_log) {
  char *gloglevel = NULL;
216
  int consolelog ;
217 218 219 220
  paramdef_t logparams_defaults[] = LOG_GLOBALPARAMS_DESC;
  paramdef_t logparams_level[MAX_LOG_PREDEF_COMPONENTS];
  paramdef_t logparams_logfile[MAX_LOG_PREDEF_COMPONENTS];
  paramdef_t logparams_debug[sizeof(log_maskmap)/sizeof(mapping)];
221
  paramdef_t logparams_dump[sizeof(log_maskmap)/sizeof(mapping)];
222
  CONFIG_SETRTFLAG(CONFIG_NOCHECKUNKOPT);
223
  int ret = config_get( logparams_defaults,sizeof(logparams_defaults)/sizeof(paramdef_t),CONFIG_STRING_LOG_PREFIX);
224

225
  if (ret <0) {
226 227 228
    fprintf(stderr,"[LOG] init aborted, configuration couldn't be performed");
    return;
  }
229

230
  /* set LOG display options (enable/disable color, thread name, level ) */
231
  for(int i=0; i<logparams_defaults[LOG_OPTIONS_IDX].numelt ; i++) {
232 233 234 235 236 237 238 239 240 241 242 243
    for(int j=0; log_options[j].name != NULL ; j++) {
      if (strcmp(logparams_defaults[LOG_OPTIONS_IDX].strlistptr[i],log_options[j].name) == 0) {
        g_log->flag = g_log->flag |  log_options[j].value;
        break;
      } else if (log_options[j+1].name == NULL) {
        fprintf(stderr,"Unknown log option: %s\n",logparams_defaults[LOG_OPTIONS_IDX].strlistptr[i]);
        exit(-1);
      }
    }
  }

  /* build the parameter array for setting per component log level and infile options */
244 245
  memset(logparams_level,    0, sizeof(paramdef_t)*MAX_LOG_PREDEF_COMPONENTS);
  memset(logparams_logfile,  0, sizeof(paramdef_t)*MAX_LOG_PREDEF_COMPONENTS);
246

247 248
  for (int i=MIN_LOG_COMPONENTS; i < MAX_LOG_PREDEF_COMPONENTS; i++) {
    if(g_log->log_component[i].name == NULL) {
249 250 251 252
      g_log->log_component[i].name = malloc(16);
      sprintf((char *)g_log->log_component[i].name,"comp%i?",i);
      logparams_logfile[i].paramflags = PARAMFLAG_DONOTREAD;
      logparams_level[i].paramflags = PARAMFLAG_DONOTREAD;
253
    }
254

255 256
    sprintf(logparams_level[i].optname,    LOG_CONFIG_LEVEL_FORMAT,       g_log->log_component[i].name);
    sprintf(logparams_logfile[i].optname,  LOG_CONFIG_LOGFILE_FORMAT,     g_log->log_component[i].name);
257 258 259 260 261 262 263 264 265 266

    /* workaround: all log options in existing configuration files use lower case component names
       where component names include uppercase char in log.h....                                */
    for (int j=0 ; j<strlen(logparams_level[i].optname); j++)
      logparams_level[i].optname[j] = tolower(logparams_level[i].optname[j]);

    for (int j=0 ; j<strlen(logparams_level[i].optname); j++)
      logparams_logfile[i].optname[j] = tolower(logparams_logfile[i].optname[j]);

    /* */
267 268 269 270 271 272 273
    logparams_level[i].defstrval     = gloglevel;
    logparams_logfile[i].defuintval  = 0;
    logparams_logfile[i].numelt      = 0;
    logparams_level[i].numelt        = 0;
    logparams_level[i].type          = TYPE_STRING;
    logparams_logfile[i].type        = TYPE_UINT;
    logparams_logfile[i].paramflags  = logparams_logfile[i].paramflags|PARAMFLAG_BOOL;
274 275 276 277 278 279 280
  }

  /* read the per component parameters */
  config_get( logparams_level,    MAX_LOG_PREDEF_COMPONENTS,CONFIG_STRING_LOG_PREFIX);
  config_get( logparams_logfile,  MAX_LOG_PREDEF_COMPONENTS,CONFIG_STRING_LOG_PREFIX);

  /* now set the log levels and infile option, according to what we read */
281
  for (int i=MIN_LOG_COMPONENTS; i < MAX_LOG_PREDEF_COMPONENTS; i++) {
282 283
    g_log->log_component[i].level = map_str_to_int(log_level_names,    *(logparams_level[i].strptr));
    set_log(i, g_log->log_component[i].level);
284

285
    if (*(logparams_logfile[i].uptr) == 1)
286
      set_component_filelog(i);
287 288
  }

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
  /* build then read the debug and dump parameter array */
  for (int i=0; log_maskmap[i].name != NULL ; i++) {
    sprintf(logparams_debug[i].optname,  LOG_CONFIG_DEBUG_FORMAT, log_maskmap[i].name);
    sprintf(logparams_dump[i].optname,   LOG_CONFIG_DUMP_FORMAT, log_maskmap[i].name);
    logparams_debug[i].defuintval  = 0;
    logparams_debug[i].type        = TYPE_UINT;
    logparams_debug[i].paramflags  = PARAMFLAG_BOOL;
    logparams_debug[i].uptr        = NULL;
    logparams_debug[i].chkPptr     = NULL;
    logparams_debug[i].numelt      = 0;
    logparams_dump[i].defuintval  = 0;
    logparams_dump[i].type        = TYPE_UINT;
    logparams_dump[i].paramflags  = PARAMFLAG_BOOL;
    logparams_dump[i].uptr        = NULL;
    logparams_dump[i].chkPptr     = NULL;
    logparams_dump[i].numelt      = 0;
305
  }
306

307
  config_get( logparams_debug,(sizeof(log_maskmap)/sizeof(mapping)) - 1 ,CONFIG_STRING_LOG_PREFIX);
308
  CONFIG_CLEARRTFLAG(CONFIG_NOCHECKUNKOPT);
309
  config_get( logparams_dump,(sizeof(log_maskmap)/sizeof(mapping)) - 1 ,CONFIG_STRING_LOG_PREFIX);
310 311

  /* set the debug mask according to the debug parameters values */
312 313
  for (int i=0; log_maskmap[i].name != NULL ; i++) {
    if (*(logparams_debug[i].uptr) )
314 315
      g_log->debug_mask = g_log->debug_mask | log_maskmap[i].value;

316
    if (*(logparams_dump[i].uptr) )
317 318 319 320
      g_log->dump_mask = g_log->dump_mask | log_maskmap[i].value;
  }

  /* log globally enabled/disabled */
321
  set_glog_onlinelog(consolelog);
322 323
}

324 325
int register_log_component(char *name, char *fext, int compidx) {
  int computed_compidx=compidx;
326 327

  if (strlen(fext) > 3) {
328
    fext[3]=0;  /* limit log file extension to 3 chars */
329
  }
330

331
  if (compidx < 0) { /* this is not a pre-defined component */
332 333 334 335
    for (int i = MAX_LOG_PREDEF_COMPONENTS; i< MAX_LOG_COMPONENTS; i++) {
      if (g_log->log_component[i].name == NULL) {
        computed_compidx=i;
        break;
336
      }
337
    }
338
  }
339

340
  if (computed_compidx >= 0 && computed_compidx <MAX_LOG_COMPONENTS) {
341 342 343 344 345
    g_log->log_component[computed_compidx].name = strdup(name);
    g_log->log_component[computed_compidx].stream = stdout;
    g_log->log_component[computed_compidx].filelog = 0;
    g_log->log_component[computed_compidx].filelog_name = malloc(strlen(name)+16);/* /tmp/<name>.%s  */
    sprintf(g_log->log_component[computed_compidx].filelog_name,"/tmp/%s.%s",name,fext);
346
  } else {
347
    fprintf(stderr,"{LOG} %s %d Couldn't register componemt %s\n",__FILE__,__LINE__,name);
348
  }
349 350

  return computed_compidx;
351 352
}

353 354 355 356 357 358 359 360
int isLogInitDone (void) {
  if (g_log == NULL)
    return 0;

  if (!(g_log->flag & FLAG_INITIALIZED))
    return 0;

  return 1;
361 362
}

363
int logInit (void) {
364 365 366 367 368 369 370 371
  int i;
  g_log = calloc(1, sizeof(log_t));

  if (g_log == NULL) {
    perror ("cannot allocated memory for log generation module \n");
    exit(EXIT_FAILURE);
  }

372
  memset(g_log,0,sizeof(log_t));
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  register_log_component("PHY","log",PHY);
  register_log_component("MAC","log",MAC);
  register_log_component("OPT","log",OPT);
  register_log_component("RLC","log",RLC);
  register_log_component("PDCP","log",PDCP);
  register_log_component("RRC","log",RRC);
  register_log_component("OMG","csv",OMG);
  register_log_component("OTG","log",OTG);
  register_log_component("OTG_LATENCY","dat",OTG_LATENCY);
  register_log_component("OTG_LATENCY_BG","dat",OTG_LATENCY_BG);
  register_log_component("OTG_GP","dat",OTG_GP);
  register_log_component("OTG_GP_BG","dat",OTG_GP_BG);
  register_log_component("OTG_JITTER","dat",OTG_JITTER);
  register_log_component("OCG","",OCG);
  register_log_component("PERF","",PERF);
388 389 390 391 392 393 394 395 396 397 398 399 400
  register_log_component("OIP","",OIP);
  register_log_component("CLI","",CLI);
  register_log_component("MSC","log",MSC);
  register_log_component("OCM","log",OCM);
  register_log_component("HW","",HW);
  register_log_component("OSA","",OSA);
  register_log_component("eRAL","",RAL_ENB);
  register_log_component("mRAL","",RAL_UE);
  register_log_component("ENB_APP","log",ENB_APP);
  register_log_component("FLEXRAN_AGENT","log",FLEXRAN_AGENT);
  register_log_component("TMR","",TMR);
  register_log_component("USIM","txt",USIM);
  register_log_component("SIM","txt",SIM);
401 402 403 404 405 406
  /* following log component are used for the localization*/
  register_log_component("LOCALIZE","log",LOCALIZE);
  register_log_component("NAS","log",NAS);
  register_log_component("UDP","",UDP_);
  register_log_component("GTPV1U","",GTPU);
  register_log_component("S1AP","",S1AP);
407
  register_log_component("X2AP","",X2AP);
408
  register_log_component("SCTP","",SCTP);
409 410 411
  register_log_component("X2AP","",X2AP);
  register_log_component("LOADER","log",LOADER);
  register_log_component("ASN","log",ASN);
412

413
  for (int i=0 ; log_level_names[i].name != NULL ; i++)
414
    g_log->level2string[i]           = toupper(log_level_names[i].name[0]); // uppercased first letter of level name
415 416 417 418 419 420

  g_log->filelog_name = "/tmp/openair.log";
  log_getconfig(g_log);

  // set all unused component items to 0, they are for non predefined components
  for (i=MAX_LOG_PREDEF_COMPONENTS; i < MAX_LOG_COMPONENTS; i++) {
421
    memset(&(g_log->log_component[i]),0,sizeof(log_component_t));
422
  }
423

424
  g_log->flag =  g_log->flag | FLAG_INITIALIZED;
425 426 427 428 429
  printf("log init done\n");
  return 0;
}


430
char *log_getthreadname(char *threadname, int bufsize) {
431
  int rt =   pthread_getname_np(pthread_self(), threadname,bufsize) ;
432

433 434 435 436 437
  if (rt == 0) {
    return threadname;
  } else {
    return "thread?";
  }
438 439
}

440
static int log_header(char *log_buffer, int buffsize, int comp, int level,const char *format) {
441
  char threadname[PR_SET_NAME];
442 443 444 445 446 447 448 449
  return  snprintf(log_buffer, buffsize , "%s%s[%s]%c %s %s%s",
                   log_level_highlight_end[level],
                   ( (g_log->flag & FLAG_NOCOLOR)?"":log_level_highlight_start[level]),
                   g_log->log_component[comp].name,
                   ( (g_log->flag & FLAG_LEVEL)?g_log->level2string[level]:' '),
                   ( (g_log->flag & FLAG_THREAD)?log_getthreadname(threadname,PR_SET_NAME+1):""),
                   format,
                   log_level_highlight_end[level]);
450
}
451

452
void logRecord_mt(const char *file, const char *func, int line, int comp, int level, const char *format, ... ) {
453 454 455
  char log_buffer[MAX_LOG_TOTAL];
  va_list args;
  va_start(args, format);
456
  log_header(log_buffer,MAX_LOG_TOTAL ,comp, level,format);
457
  g_log->log_component[comp].vprint(g_log->log_component[comp].stream,log_buffer, args);
458 459 460
  va_end(args);
}

461 462 463
void log_dump(int component, void *buffer, int buffsize,int datatype, const char *format, ... ) {
  va_list args;
  char *wbuf;
464

465 466 467 468 469 470 471 472 473 474
  switch(datatype) {
    case LOG_DUMP_DOUBLE:
      wbuf=malloc((buffsize * 10)  + 64 + MAX_LOG_TOTAL);
      break;

    case LOG_DUMP_CHAR:
    default:
      wbuf=malloc((buffsize * 3 ) + 64 + MAX_LOG_TOTAL);
      break;
  }
475

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
  if (wbuf != NULL) {
    va_start(args, format);
    int pos=log_header(wbuf,MAX_LOG_TOTAL ,component, OAILOG_INFO,"");
    int pos2=vsprintf(wbuf+pos,format, args);
    pos=pos+pos2;
    va_end(args);

    for (int i=0; i<buffsize; i++) {
      switch(datatype) {
        case LOG_DUMP_DOUBLE:
          pos = pos + sprintf(wbuf+pos,"%04.4lf ", (double)((double *)buffer)[i]);
          break;

        case LOG_DUMP_CHAR:
        default:
          pos = pos + sprintf(wbuf+pos,"%02x ", (unsigned char)((unsigned char *)buffer)[i]);
          break;
      }
494
    }
495

496 497 498
    sprintf(wbuf+pos,"\n");
    g_log->log_component[component].print(g_log->log_component[component].stream,wbuf);
    free(wbuf);
499
  }
500
}
501

502
int set_log(int component, int level) {
503 504 505
  /* Checking parameters */
  DevCheck((component >= MIN_LOG_COMPONENTS) && (component < MAX_LOG_COMPONENTS),
           component, MIN_LOG_COMPONENTS, MAX_LOG_COMPONENTS);
506
  DevCheck((level < NUM_LOG_LEVEL) && (level >= OAILOG_DISABLE), level, NUM_LOG_LEVEL,
507 508
           OAILOG_ERR);

509
  if ( g_log->log_component[component].level != OAILOG_DISABLE )
510
    g_log->log_component[component].savedlevel = g_log->log_component[component].level;
511

512
  g_log->log_component[component].level = level;
513 514 515 516 517
  return 0;
}



518
void set_glog(int level) {
519
  for (int c=0; c< MAX_LOG_COMPONENTS; c++ ) {
520
    set_log(c, level);
521 522 523
  }
}

524
void set_glog_onlinelog(int enable) {
525
  for (int c=0; c< MAX_LOG_COMPONENTS; c++ ) {
526 527 528 529 530 531 532 533 534
    if ( enable ) {
      g_log->log_component[c].level = g_log->log_component[c].savedlevel;
      g_log->log_component[c].vprint = vfprintf;
      g_log->log_component[c].print = fprintf;
      g_log->log_component[c].stream = stdout;
    } else {
      g_log->log_component[c].level = OAILOG_DISABLE;
    }
  }
535
}
536 537
void set_glog_filelog(int enable) {
  static FILE *fptr;
538 539 540 541 542 543 544 545 546 547 548 549

  if ( enable ) {
    fptr = fopen(g_log->filelog_name,"w");

    for (int c=0; c< MAX_LOG_COMPONENTS; c++ ) {
      close_component_filelog(c);
      g_log->log_component[c].stream = fptr;
      g_log->log_component[c].filelog =  1;
    }
  } else {
    for (int c=0; c< MAX_LOG_COMPONENTS; c++ ) {
      g_log->log_component[c].filelog =  0;
550

551 552 553
      if (fptr != NULL) {
        fclose(fptr);
      }
554

555
      g_log->log_component[c].stream = stdout;
556 557
    }
  }
558 559
}

560 561 562 563 564 565 566 567
void set_component_filelog(int comp) {
  if (g_log->log_component[comp].stream == NULL || g_log->log_component[comp].stream == stdout) {
    g_log->log_component[comp].stream = fopen(g_log->log_component[comp].filelog_name,"w");
  }

  g_log->log_component[comp].vprint = vfprintf;
  g_log->log_component[comp].print = fprintf;
  g_log->log_component[comp].filelog =  1;
568
}
569 570 571 572 573 574 575 576 577 578
void close_component_filelog(int comp) {
  g_log->log_component[comp].filelog =  0;

  if (g_log->log_component[comp].stream != NULL && g_log->log_component[comp].stream != stdout ) {
    fclose(g_log->log_component[comp].stream);
    g_log->log_component[comp].stream = stdout;
  }

  g_log->log_component[comp].vprint = vfprintf;
  g_log->log_component[comp].print = fprintf;
579 580 581 582 583 584 585
}

/*
 * for the two functions below, the passed array must have a final entry
 * with string value NULL
 */
/* map a string to an int. Takes a mapping array and a string as arg */
586
int map_str_to_int(mapping *map, const char *str) {
587 588 589 590 591 592 593 594 595 596 597 598 599 600
  while (1) {
    if (map->name == NULL) {
      return(-1);
    }

    if (!strcmp(map->name, str)) {
      return(map->value);
    }

    map++;
  }
}

/* map an int to a string. Takes a mapping array and a value */
601
char *map_int_to_str(mapping *map, int val) {
602 603 604 605 606 607 608 609 610 611 612 613 614
  while (1) {
    if (map->name == NULL) {
      return NULL;
    }

    if (map->value == val) {
      return map->name;
    }

    map++;
  }
}

615
int is_newline( char *str, int size) {
616 617 618 619 620 621 622 623 624 625 626 627
  int i;

  for (  i = 0; i < size; i++ ) {
    if ( str[i] == '\n' ) {
      return 1;
    }
  }

  /* if we get all the way to here, there must not have been a newline! */
  return 0;
}

628
void logClean (void) {
629 630
  int i;

631 632
  if(isLogInitDone()) {
    LOG_UI(PHY,"\n");
633

634 635 636
    for (i=MIN_LOG_COMPONENTS; i < MAX_LOG_COMPONENTS; i++) {
      close_component_filelog(i);
    }
637 638 639 640
  }
}


641
#ifdef LOG_TEST
642

643
int main(int argc, char *argv[]) {
644 645 646 647 648
  logInit();
  test_log();
  return 1;
}

649
int test_log(void) {
650 651 652 653 654
  LOG_ENTER(MAC); // because the default level is DEBUG
  LOG_I(EMU, "1 Starting OAI logs version %s Build date: %s on %s\n",
        BUILD_VERSION, BUILD_DATE, BUILD_HOST);
  LOG_D(MAC, "1 debug  MAC \n");
  LOG_W(MAC, "1 warning MAC \n");
655 656
  set_log(EMU, OAILOG_INFO);
  set_log(MAC, OAILOG_WARNING);
657 658 659 660 661 662
  LOG_I(EMU, "2 Starting OAI logs version %s Build date: %s on %s\n",
        BUILD_VERSION, BUILD_DATE, BUILD_HOST);
  LOG_E(MAC, "2 error MAC\n");
  LOG_D(MAC, "2 debug  MAC \n");
  LOG_W(MAC, "2 warning MAC \n");
  LOG_I(MAC, "2 info MAC \n");
663
  set_log(MAC, OAILOG_NOTICE);
664 665 666 667 668 669
  LOG_ENTER(MAC);
  LOG_I(EMU, "3 Starting OAI logs version %s Build date: %s on %s\n",
        BUILD_VERSION, BUILD_DATE, BUILD_HOST);
  LOG_D(MAC, "3 debug  MAC \n");
  LOG_W(MAC, "3 warning MAC \n");
  LOG_I(MAC, "3 info MAC \n");
670 671
  set_log(MAC, LOG_DEBUG);
  set_log(EMU, LOG_DEBUG);
672 673 674 675 676 677
  LOG_ENTER(MAC);
  LOG_I(EMU, "4 Starting OAI logs version %s Build date: %s on %s\n",
        BUILD_VERSION, BUILD_DATE, BUILD_HOST);
  LOG_D(MAC, "4 debug  MAC \n");
  LOG_W(MAC, "4 warning MAC \n");
  LOG_I(MAC, "4 info MAC \n");
678 679
  set_log(MAC, LOG_DEBUG);
  set_log(EMU, LOG_DEBUG);
680 681 682 683 684
  LOG_I(LOG, "5 Starting OAI logs version %s Build date: %s on %s\n",
        BUILD_VERSION, BUILD_DATE, BUILD_HOST);
  LOG_D(MAC, "5 debug  MAC \n");
  LOG_W(MAC, "5 warning MAC \n");
  LOG_I(MAC, "5 info MAC \n");
685 686
  set_log(MAC, LOG_TRACE);
  set_log(EMU, LOG_TRACE);
687 688 689 690 691 692 693 694 695 696
  LOG_ENTER(MAC);
  LOG_I(LOG, "6 Starting OAI logs version %s Build date: %s on %s\n",
        BUILD_VERSION, BUILD_DATE, BUILD_HOST);
  LOG_D(MAC, "6 debug  MAC \n");
  LOG_W(MAC, "6 warning MAC \n");
  LOG_I(MAC, "6 info MAC \n");
  LOG_EXIT(MAC);
  return 0;
}
#endif