log.c 51.3 KB
Newer Older
1 2 3 4 5
/*
 * 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
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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
 */
21

22 23 24
/*! \file log.c
* \brief log implementaion
* \author Navid Nikaein
25
* \date 2009 - 2014
26 27 28 29 30
* \version 0.5
* @ingroup util

*/

31
#define _GNU_SOURCE  /* required for pthread_getname_np */
32 33 34 35
//#define LOG_TEST 1

#define COMPONENT_LOG
#define COMPONENT_LOG_IF
36
#include <ctype.h>
37
#define LOG_MAIN
38 39
#include "log.h"
#include "vcd_signal_dumper.h"
40
#include "assertions.h"
41

42 43 44 45
#if defined(ENABLE_ITTI)
# include "intertask_interface.h"
#endif

Cedric Roux's avatar
Cedric Roux committed
46 47
# include <pthread.h>
# include <string.h>
48
#include "common/config/config_userapi.h"
49
// main log variables
50

51

52 53 54 55 56 57 58 59 60 61 62 63
mapping log_level_names[] = {
  {"emerg", LOG_EMERG},
  {"alert", LOG_ALERT},
  {"crit", LOG_CRIT},
  {"error", LOG_ERR},
  {"warn", LOG_WARNING},
  {"notice", LOG_NOTICE},
  {"info", LOG_INFO},
  {"debug", LOG_DEBUG},
  {"file", LOG_FILE},
  {"trace", LOG_TRACE},
  {NULL, -1}
64
};
65
mapping log_verbosity_names[] = {
66 67 68 69 70 71 72 73
  {"none", 0x0},
  {"low", 0x5},
  {"medium", 0x15},
  {"high", 0x35},
  {"full", 0x75},
  {NULL, -1}
};

74 75 76 77
// vars for the log thread
LOG_params log_list[2000];
int log_list_tail = 0;
int log_list_nb_elements = 0;
78

79 80
pthread_mutex_t log_lock;
pthread_cond_t log_notify;
81

82 83 84 85
#if !defined(LOG_NO_THREAD)
int log_list_head = 0;
int log_shutdown;
#endif
86

87
static int gfd;
88

89 90
static char *log_level_highlight_start[] = {LOG_RED, LOG_RED, LOG_RED, LOG_RED, LOG_ORANGE, LOG_BLUE, "", ""};  /*!< \brief Optional start-format strings for highlighting */
static char *log_level_highlight_end[]   = {LOG_RESET, LOG_RESET, LOG_RESET, LOG_RESET, LOG_RESET,LOG_RESET,  "",""};   /*!< \brief Optional end-format strings for highlighting */
91

92 93 94 95
#if defined(ENABLE_ITTI)
static log_instance_type_t log_instance_type;
#endif

96 97 98 99 100 101 102 103 104 105
/* get log parameters from configuration file */
void  log_getconfig(log_t *g_log) {
  char *gloglevel = NULL;
  char *glogverbo = NULL;
  int level,verbosity;
  paramdef_t logparams_defaults[] = LOG_GLOBALPARAMS_DESC;
  paramdef_t logparams_level[MAX_LOG_COMPONENTS];
  paramdef_t logparams_verbosity[MAX_LOG_COMPONENTS];
  paramdef_t logparams_logfile[MAX_LOG_COMPONENTS];
  
106 107 108 109 110
  int ret = config_get( logparams_defaults,sizeof(logparams_defaults)/sizeof(paramdef_t),CONFIG_STRING_LOG_PREFIX);
  if (ret <0) {
       fprintf(stderr,"[LOG] init aborted, configuration couldn't be performed");
       return;
  } 
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
  memset(logparams_level,    0, sizeof(paramdef_t)*MAX_LOG_COMPONENTS);
  memset(logparams_verbosity,0, sizeof(paramdef_t)*MAX_LOG_COMPONENTS);
  memset(logparams_logfile,  0, sizeof(paramdef_t)*MAX_LOG_COMPONENTS);
  for (int i=MIN_LOG_COMPONENTS; i < MAX_LOG_COMPONENTS; i++) {
    if(g_log->log_component[i].name == NULL) {
       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;
       logparams_verbosity[i].paramflags = PARAMFLAG_DONOTREAD;
    }
    sprintf(logparams_level[i].optname,    LOG_CONFIG_LEVEL_FORMAT,       g_log->log_component[i].name);
    sprintf(logparams_verbosity[i].optname,LOG_CONFIG_VERBOSITY_FORMAT,   g_log->log_component[i].name);
    sprintf(logparams_logfile[i].optname,  LOG_CONFIG_LOGFILE_FORMAT,     g_log->log_component[i].name);
/* 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_verbosity[i].optname[j] = tolower(logparams_verbosity[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]);
/* */
    logparams_level[i].defstrval     = gloglevel;
    logparams_verbosity[i].defstrval = glogverbo; 

    logparams_level[i].type          = TYPE_STRING;
    logparams_verbosity[i].type      = TYPE_STRING;
    logparams_logfile[i].type        = TYPE_UINT;

    logparams_logfile[i].paramflags  = logparams_logfile[i].paramflags|PARAMFLAG_BOOL;
    }
  config_get( logparams_level,    MAX_LOG_COMPONENTS,CONFIG_STRING_LOG_PREFIX); 
  config_get( logparams_verbosity,MAX_LOG_COMPONENTS,CONFIG_STRING_LOG_PREFIX); 
  config_get( logparams_logfile,  MAX_LOG_COMPONENTS,CONFIG_STRING_LOG_PREFIX); 
  for (int i=MIN_LOG_COMPONENTS; i < MAX_LOG_COMPONENTS; i++) {
    verbosity = map_str_to_int(log_verbosity_names,*(logparams_verbosity[i].strptr));
    level     = map_str_to_int(log_level_names,    *(logparams_level[i].strptr));
    set_comp_log(i, level,verbosity,1);
    set_component_filelog(*(logparams_logfile[i].uptr));
    }
}


155 156
int logInit (void)
{
157 158
  int i;
  g_log = calloc(1, sizeof(log_t));
Cedric Roux's avatar
Cedric Roux committed
159

160 161 162 163
  if (g_log == NULL) {
    perror ("cannot allocated memory for log generation module \n");
    exit(EXIT_FAILURE);
  }
164 165


166
#if ! defined(CN_BUILD)
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
  g_log->log_component[PHY].name = "PHY";
  g_log->log_component[PHY].level = LOG_EMERG;
  g_log->log_component[PHY].flag =  LOG_MED;
  g_log->log_component[PHY].interval =  1;
  g_log->log_component[PHY].fd = 0;
  g_log->log_component[PHY].filelog = 0;
  g_log->log_component[PHY].filelog_name = "/tmp/phy.log";

  g_log->log_component[MAC].name = "MAC";
  g_log->log_component[MAC].level = LOG_EMERG;
  g_log->log_component[MAC].flag =  LOG_MED;
  g_log->log_component[MAC].interval =  1;
  g_log->log_component[MAC].fd = 0;
  g_log->log_component[MAC].filelog = 0;
  g_log->log_component[MAC].filelog_name = "/tmp/mac.log";

  g_log->log_component[OPT].name = "OPT";
  g_log->log_component[OPT].level = LOG_EMERG;
  g_log->log_component[OPT].flag = LOG_MED;
  g_log->log_component[OPT].interval =  1;
  g_log->log_component[OPT].fd = 0;
  g_log->log_component[OPT].filelog = 0;
  g_log->log_component[OPT].filelog_name = "";

  g_log->log_component[RLC].name = "RLC";
  g_log->log_component[RLC].level = LOG_INFO;
  g_log->log_component[RLC].flag = LOG_MED;
  g_log->log_component[RLC].interval =  1;
  g_log->log_component[RLC].fd = 0;
  g_log->log_component[RLC].filelog = 0;
  g_log->log_component[RLC].filelog_name = "/tmp/rlc.log";

  g_log->log_component[PDCP].name = "PDCP";
  g_log->log_component[PDCP].level = LOG_INFO;
  g_log->log_component[PDCP].flag = LOG_MED;
  g_log->log_component[PDCP].interval =  1;
  g_log->log_component[PDCP].fd = 0;
  g_log->log_component[PDCP].filelog = 0;
  g_log->log_component[PDCP].filelog_name = "/tmp/pdcp.log";

  g_log->log_component[RRC].name = "RRC";
  g_log->log_component[RRC].level = LOG_TRACE;
  g_log->log_component[RRC].flag = LOG_MED;
  g_log->log_component[RRC].interval =  1;
  g_log->log_component[RRC].fd = 0;
  g_log->log_component[RRC].filelog = 0;
  g_log->log_component[RRC].filelog_name = "/tmp/rrc.log";

215 216 217 218 219 220 221
  g_log->log_component[SIM].name = "SIM";
  g_log->log_component[SIM].level = LOG_EMERG;
  g_log->log_component[SIM].flag =  LOG_MED;
  g_log->log_component[SIM].interval =  1;
  g_log->log_component[SIM].fd = 0;
  g_log->log_component[SIM].filelog = 0;
  g_log->log_component[SIM].filelog_name = "";
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

  g_log->log_component[OMG].name = "OMG";
  g_log->log_component[OMG].level = LOG_EMERG;
  g_log->log_component[OMG].flag =  LOG_MED;
  g_log->log_component[OMG].interval =  1;
  g_log->log_component[OMG].fd = 0;
  g_log->log_component[OMG].filelog = 0;
  g_log->log_component[OMG].filelog_name = "/tmp/omg.csv";

  g_log->log_component[OTG].name = "OTG";
  g_log->log_component[OTG].level = LOG_EMERG;
  g_log->log_component[OTG].flag =  LOG_MED;
  g_log->log_component[OTG].interval =  1;
  g_log->log_component[OTG].fd = 0;
  g_log->log_component[OTG].filelog = 0;
  g_log->log_component[OTG].filelog_name = "/tmp/otg.log";

  g_log->log_component[OTG_LATENCY].name = "OTG_LATENCY";
  g_log->log_component[OTG_LATENCY].level = LOG_EMERG;
  g_log->log_component[OTG_LATENCY].flag =  LOG_MED;
  g_log->log_component[OTG_LATENCY].interval =  1;
  g_log->log_component[OTG_LATENCY].fd = 0;
  g_log->log_component[OTG_LATENCY].filelog = 0;
  g_log->log_component[OTG_LATENCY].filelog_name = "/tmp/otg_latency.dat";

  g_log->log_component[OTG_LATENCY_BG].name = "OTG_LATENCY_BG";
  g_log->log_component[OTG_LATENCY_BG].level = LOG_EMERG;
  g_log->log_component[OTG_LATENCY_BG].flag =  LOG_MED;
  g_log->log_component[OTG_LATENCY_BG].interval =  1;
  g_log->log_component[OTG_LATENCY_BG].fd = 0;
  g_log->log_component[OTG_LATENCY_BG].filelog = 0;
  g_log->log_component[OTG_LATENCY_BG].filelog_name = "/tmp/otg_latency_bg.dat";

  g_log->log_component[OTG_GP].name = "OTG_GP";
  g_log->log_component[OTG_GP].level = LOG_EMERG;
  g_log->log_component[OTG_GP].flag =  LOG_MED;
  g_log->log_component[OTG_GP].interval =  1;
  g_log->log_component[OTG_GP].fd = 0;
  g_log->log_component[OTG_GP].filelog = 0;
  g_log->log_component[OTG_GP].filelog_name = "/tmp/otg_goodput.dat";

  g_log->log_component[OTG_GP_BG].name = "OTG_GP_BG";
  g_log->log_component[OTG_GP_BG].level = LOG_EMERG;
  g_log->log_component[OTG_GP_BG].flag =  LOG_MED;
  g_log->log_component[OTG_GP_BG].interval =  1;
  g_log->log_component[OTG_GP_BG].fd = 0;
  g_log->log_component[OTG_GP_BG].filelog = 0;
  g_log->log_component[OTG_GP_BG].filelog_name = "/tmp/otg_goodput_bg.dat";

  g_log->log_component[OTG_JITTER].name = "OTG_JITTER";
  g_log->log_component[OTG_JITTER].level = LOG_EMERG;
  g_log->log_component[OTG_JITTER].flag =  LOG_MED;
  g_log->log_component[OTG_JITTER].interval =  1;
  g_log->log_component[OTG_JITTER].fd = 0;
  g_log->log_component[OTG_JITTER].filelog = 0;
  g_log->log_component[OTG_JITTER].filelog_name = "/tmp/otg_jitter.dat";

  g_log->log_component[OCG].name = "OCG";
  g_log->log_component[OCG].level = LOG_EMERG;
  g_log->log_component[OCG].flag =  LOG_MED;
  g_log->log_component[OCG].interval =  1;
  g_log->log_component[OCG].fd = 0;
  g_log->log_component[OCG].filelog = 0;
  g_log->log_component[OCG].filelog_name = "";

  g_log->log_component[PERF].name = "PERF";
  g_log->log_component[PERF].level = LOG_EMERG;
  g_log->log_component[PERF].flag =  LOG_MED;
  g_log->log_component[PERF].interval =  1;
  g_log->log_component[PERF].fd = 0;
  g_log->log_component[PERF].filelog = 0;
  g_log->log_component[PERF].filelog_name = "";

  g_log->log_component[OIP].name = "OIP";
  g_log->log_component[OIP].level = LOG_EMERG;
  g_log->log_component[OIP].flag =  LOG_MED;
  g_log->log_component[OIP].interval =  1;
  g_log->log_component[OIP].fd = 0;
  g_log->log_component[OIP].filelog = 0;
  g_log->log_component[OIP].filelog_name = "";

  g_log->log_component[CLI].name = "CLI";
  g_log->log_component[CLI].level = LOG_EMERG;
  g_log->log_component[CLI].flag =  LOG_MED;
  g_log->log_component[CLI].interval =  1;
  g_log->log_component[CLI].fd = 0;
  g_log->log_component[CLI].filelog =  0;
  g_log->log_component[CLI].filelog_name = "";

  g_log->log_component[MSC].name = "MSC";
  g_log->log_component[MSC].level = LOG_EMERG;
  g_log->log_component[MSC].flag =  LOG_MED;
  g_log->log_component[MSC].interval =  1;
  g_log->log_component[MSC].fd = 0;
  g_log->log_component[MSC].filelog =  0;
  g_log->log_component[MSC].filelog_name = "/tmp/msc.log";

  g_log->log_component[OCM].name = "OCM";
  g_log->log_component[OCM].level = LOG_EMERG;
  g_log->log_component[OCM].flag =  LOG_MED;
  g_log->log_component[OCM].interval =  1;
  g_log->log_component[OCM].fd = 0;
  g_log->log_component[OCM].filelog =  0;
  g_log->log_component[OCM].filelog_name = "/tmp/ocm.log";

  g_log->log_component[HW].name = "HW";
  g_log->log_component[HW].level = LOG_EMERG;
  g_log->log_component[HW].flag = LOG_MED;
  g_log->log_component[HW].interval = 1;
  g_log->log_component[HW].fd = 0;
  g_log->log_component[HW].filelog = 0;
  g_log->log_component[HW].filelog_name = "";

  g_log->log_component[OSA].name = "OSA";
  g_log->log_component[OSA].level = LOG_EMERG;
  g_log->log_component[OSA].flag = LOG_MED;
  g_log->log_component[OSA].interval = 1;
  g_log->log_component[OSA].fd = 0;
  g_log->log_component[OSA].filelog = 0;
  g_log->log_component[OSA].filelog_name = "";

  g_log->log_component[RAL_ENB].name = "eRAL";
  g_log->log_component[RAL_ENB].level = LOG_EMERG;
  g_log->log_component[RAL_ENB].flag = LOG_MED;
  g_log->log_component[RAL_ENB].interval = 1;
  g_log->log_component[RAL_ENB].fd = 0;
  g_log->log_component[RAL_ENB].filelog = 0;
  g_log->log_component[RAL_ENB].filelog_name = "";

  g_log->log_component[RAL_UE].name = "mRAL";
  g_log->log_component[RAL_UE].level = LOG_EMERG;
  g_log->log_component[RAL_UE].flag = LOG_MED;
  g_log->log_component[RAL_UE].interval = 1;
  g_log->log_component[RAL_UE].fd = 0;
  g_log->log_component[RAL_UE].filelog = 0;
  g_log->log_component[RAL_UE].filelog_name = "";

  g_log->log_component[ENB_APP].name = "ENB_APP";
  g_log->log_component[ENB_APP].level = LOG_EMERG;
  g_log->log_component[ENB_APP].flag = LOG_MED;
  g_log->log_component[ENB_APP].interval = 1;
  g_log->log_component[ENB_APP].fd = 0;
  g_log->log_component[ENB_APP].filelog = 0;
  g_log->log_component[ENB_APP].filelog_name = "";

367 368 369 370 371 372 373
  g_log->log_component[FLEXRAN_AGENT].name = "FLEXRAN_AGENT";
  g_log->log_component[FLEXRAN_AGENT].level = LOG_DEBUG;
  g_log->log_component[FLEXRAN_AGENT].flag = LOG_MED;
  g_log->log_component[FLEXRAN_AGENT].interval = 1;
  g_log->log_component[FLEXRAN_AGENT].fd = 0;
  g_log->log_component[FLEXRAN_AGENT].filelog = 0;
  g_log->log_component[FLEXRAN_AGENT].filelog_name = "";
374
  
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
  g_log->log_component[TMR].name = "TMR";
  g_log->log_component[TMR].level = LOG_EMERG;
  g_log->log_component[TMR].flag = LOG_MED;
  g_log->log_component[TMR].interval = 1;
  g_log->log_component[TMR].fd = 0;
  g_log->log_component[TMR].filelog = 0;
  g_log->log_component[TMR].filelog_name = "";

  g_log->log_component[USIM].name = "USIM";
  g_log->log_component[USIM].level = LOG_DEBUG;
  g_log->log_component[USIM].flag = LOG_NONE;
  g_log->log_component[USIM].interval = 1;
  g_log->log_component[USIM].fd = 0;
  g_log->log_component[USIM].filelog = 0;
  g_log->log_component[USIM].filelog_name = "/tmp/usim.txt";

  /* following log component are used for the localization*/
  g_log->log_component[LOCALIZE].name = "LOCALIZE";
  g_log->log_component[LOCALIZE].level = LOG_EMERG;
  g_log->log_component[LOCALIZE].flag =  LOG_MED;
  g_log->log_component[LOCALIZE].interval =  1;
  g_log->log_component[LOCALIZE].fd = 0;
  g_log->log_component[LOCALIZE].filelog = 0;
  g_log->log_component[LOCALIZE].filelog_name = "/tmp/localize.log";
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
#endif // ! defined(CN_BUILD)

  g_log->log_component[NAS].name = "NAS";
  g_log->log_component[NAS].level = LOG_TRACE;
  g_log->log_component[NAS].flag = LOG_MED;
  g_log->log_component[NAS].interval =  1;
  g_log->log_component[NAS].fd = 0;
  g_log->log_component[NAS].filelog = 0;
  g_log->log_component[NAS].filelog_name = "/tmp/nas.log";

  g_log->log_component[UDP_].name = "UDP";
  g_log->log_component[UDP_].level = LOG_EMERG;
  g_log->log_component[UDP_].flag = LOG_FULL;
  g_log->log_component[UDP_].interval = 1;
  g_log->log_component[UDP_].fd = 0;
  g_log->log_component[UDP_].filelog = 0;
  g_log->log_component[UDP_].filelog_name = "";

  g_log->log_component[GTPU].name = "GTPV1U";
  g_log->log_component[GTPU].level = LOG_EMERG;
  g_log->log_component[GTPU].flag = LOG_FULL;
  g_log->log_component[GTPU].interval = 1;
  g_log->log_component[GTPU].fd = 0;
  g_log->log_component[GTPU].filelog = 0;
  g_log->log_component[GTPU].filelog_name = "";

  g_log->log_component[S1AP].name = "S1AP";
  g_log->log_component[S1AP].level = LOG_EMERG;
  g_log->log_component[S1AP].flag = LOG_FULL;
  g_log->log_component[S1AP].interval = 1;
  g_log->log_component[S1AP].fd = 0;
  g_log->log_component[S1AP].filelog = 0;
  g_log->log_component[S1AP].filelog_name = "";

  g_log->log_component[SCTP].name = "SCTP";
  g_log->log_component[SCTP].level = LOG_EMERG;
  g_log->log_component[SCTP].flag = LOG_MED;
  g_log->log_component[SCTP].interval = 1;
  g_log->log_component[SCTP].fd = 0;
  g_log->log_component[SCTP].filelog = 0;
  g_log->log_component[SCTP].filelog_name = "";
440 441 442 443 444 445 446 447 448
 
  g_log->log_component[RRH].name = "RRH";
  g_log->log_component[RRH].level = LOG_EMERG;
  g_log->log_component[RRH].flag = LOG_MED;
  g_log->log_component[RRH].interval = 1;
  g_log->log_component[RRH].fd = 0;
  g_log->log_component[RRH].filelog = 0;
  g_log->log_component[RRH].filelog_name = "";
  
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
  g_log->level2string[LOG_EMERG]         = "G"; //EMERG
  g_log->level2string[LOG_ALERT]         = "A"; // ALERT
  g_log->level2string[LOG_CRIT]          = "C"; // CRITIC
  g_log->level2string[LOG_ERR]           = "E"; // ERROR
  g_log->level2string[LOG_WARNING]       = "W"; // WARNING
  g_log->level2string[LOG_NOTICE]        = "N"; // NOTICE
  g_log->level2string[LOG_INFO]          = "I"; //INFO
  g_log->level2string[LOG_DEBUG]         = "D"; // DEBUG
  g_log->level2string[LOG_FILE]          = "F"; // file
  g_log->level2string[LOG_TRACE]         = "T"; // TRACE

  g_log->onlinelog = 1; //online log file
  g_log->syslog = 0;
  g_log->filelog   = 0;
  g_log->level  = LOG_TRACE;
  g_log->flag   = LOG_LOW;
465

466 467 468 469 470 471
  g_log->config.remote_ip      = 0;
  g_log->config.remote_level   = LOG_EMERG;
  g_log->config.facility       = LOG_LOCAL7;
  g_log->config.audit_ip       = 0;
  g_log->config.audit_facility = LOG_LOCAL6;
  g_log->config.format         = 0x00; // online debug inactive
472

473
  g_log->filelog_name = "/tmp/openair.log";
474

475
  if (g_log->syslog) {
476
#if ! defined(CN_BUILD)
477
    openlog(g_log->log_component[SIM].name, LOG_PID, g_log->config.facility);
478
#endif // ! defined(CN_BUILD)
479
  }
480
  log_getconfig(g_log);
481 482 483
  if (g_log->filelog) {
    gfd = open(g_log->filelog_name, O_WRONLY | O_CREAT, 0666);
  }
484

485 486 487 488 489
  // could put a loop here to check for all comps
  for (i=MIN_LOG_COMPONENTS; i < MAX_LOG_COMPONENTS; i++) {
    if (g_log->log_component[i].filelog == 1 ) {
      g_log->log_component[i].fd = open(g_log->log_component[i].filelog_name,
                                        O_WRONLY | O_CREAT | O_APPEND, 0666);
490
    }
491 492 493
  }

  printf("log init done\n");
494

495
  return 0;
496 497
}

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
void nfapi_log(char *file, char *func, int line, int comp, int level, const char* format, va_list args)
{
  //logRecord_mt(file,func,line, pthread_self(), comp, level, format, ##args)
  int len = 0;
  log_component_t *c;
  char *log_start;
  char *log_end;
  /* The main difference with the version above is the use of this local log_buffer.
   * The other difference is the return value of snprintf which was not used
   * correctly. It was not a big problem because in practice MAX_LOG_TOTAL is
   * big enough so that the buffer is never full.
   */
  char log_buffer[MAX_LOG_TOTAL];

  /* for no gcc warnings */
  (void)log_start;
  (void)log_end;

  c = &g_log->log_component[comp];

  // do not apply filtering for LOG_F
  // only log messages which are enabled and are below the global log level and component's level threshold
  if ((level != LOG_FILE) && ((level > c->level) || (level > g_log->level))) {
    /* if ((level != LOG_FILE) &&
          ((level > c->level) ||
           (level > g_log->level) ||
           ( c->level > g_log->level))) {
    */
    return;
  }

  //VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_LOG_RECORD, VCD_FUNCTION_IN);

  // adjust syslog level for TRACE messages
  if (g_log->syslog) {
    if (g_log->level > LOG_DEBUG) {
      g_log->level = LOG_DEBUG;
    }
  }

  // make sure that for log trace the extra info is only printed once, reset when the level changes
  if ((level == LOG_FILE) || (c->flag == LOG_NONE) || (level == LOG_TRACE)) {
    log_start = log_buffer;
    len = vsnprintf(log_buffer, MAX_LOG_TOTAL, format, args);
    if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    log_end = log_buffer + len;
  } else {
    if ( (g_log->flag & FLAG_COLOR) || (c->flag & FLAG_COLOR) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "%s",
                      log_level_highlight_start[level]);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }

    log_start = log_buffer + len;

    if ( (g_log->flag & FLAG_COMP) || (c->flag & FLAG_COMP) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "[%s]",
                      g_log->log_component[comp].name);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }

    if ( (g_log->flag & FLAG_LEVEL) || (c->flag & FLAG_LEVEL) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "[%s]",
                      g_log->level2string[level]);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }

    if ( (g_log->flag & FLAG_THREAD) || (c->flag & FLAG_THREAD) ) {
#     define THREAD_NAME_LEN 128
      char threadname[THREAD_NAME_LEN];
      if (pthread_getname_np(pthread_self(), threadname, THREAD_NAME_LEN) != 0)
      {
        perror("pthread_getname_np : ");
      } else {
        len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "[%s]", threadname);
        if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
      }
#     undef THREAD_NAME_LEN
    }

    if ( (g_log->flag & FLAG_FUNCT) || (c->flag & FLAG_FUNCT) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "[%s] ",
                      func);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }

    if ( (g_log->flag & FLAG_FILE_LINE) || (c->flag & FLAG_FILE_LINE) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "[%s:%d]",
                      file, line);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }

    //len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "[%08lx]", thread_id);
    //if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;

    len += vsnprintf(&log_buffer[len], MAX_LOG_TOTAL - len, format, args);
    if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    log_end = log_buffer + len;

    if ( (g_log->flag & FLAG_COLOR) || (c->flag & FLAG_COLOR) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "%s",
          log_level_highlight_end[level]);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }
  }

  va_end(args);

  // OAI printf compatibility
  if ((g_log->onlinelog == 1) && (level != LOG_FILE))
#ifdef RTAI
    if (len > MAX_LOG_TOTAL) {
      rt_printk ("[OPENAIR] FIFO_PRINTF WROTE OUTSIDE ITS MEMORY BOUNDARY : ERRORS WILL OCCUR\n");
    }

  if (len > 0) {
    rtf_put (FIFO_PRINTF_NO, log_buffer, len);
  }

#else
  fwrite(log_buffer, len, 1, stdout);
#endif

#ifndef RTAI

  if (g_log->syslog) {
    syslog(g_log->level, "%s", log_buffer);
  }

  if (g_log->filelog) {
    if (write(gfd, log_buffer, len) < len) {
      // TODO assert ?
    }
  }

  if ((g_log->log_component[comp].filelog) && (level == LOG_FILE)) {
    if (write(g_log->log_component[comp].fd, log_buffer, len) < len) {
      // TODO assert ?
    }
  }

#else

  // online print messges
  if ((g_log->log_component[comp].filelog) && (level == LOG_FILE)) {
    printf(log_buffer);
  }

#endif

#if defined(ENABLE_ITTI)

  if (level <= LOG_DEBUG) {
    task_id_t origin_task_id = TASK_UNKNOWN;
    MessagesIds messages_id;
    MessageDef *message_p;
    size_t      message_string_size;
    char       *message_msg_p;

    message_string_size = log_end - log_start;

#if !defined(DISABLE_ITTI_DETECT_SUB_TASK_ID)

    /* Try to identify sub task ID from log information (comp, log_instance_type) */
    switch (comp) {
    case PHY:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_PHY_ENB;
        break;

      case LOG_INSTANCE_UE:
        origin_task_id = TASK_PHY_UE;
        break;

      default:
        break;
      }

      break;

    case MAC:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_MAC_ENB;
        break;

      case LOG_INSTANCE_UE:
        origin_task_id = TASK_MAC_UE;

      default:
        break;
      }

      break;

    case RLC:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_RLC_ENB;
        break;

      case LOG_INSTANCE_UE:
        origin_task_id = TASK_RLC_UE;

      default:
        break;
      }

      break;

    case PDCP:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_PDCP_ENB;
        break;

      case LOG_INSTANCE_UE:
        origin_task_id = TASK_PDCP_UE;

      default:
        break;
      }

      break;

    default:
      break;
    }

#endif

    switch (level) {
    case LOG_EMERG:
    case LOG_ALERT:
    case LOG_CRIT:
    case LOG_ERR:
      messages_id = ERROR_LOG;
      break;

    case LOG_WARNING:
      messages_id = WARNING_LOG;
      break;

    case LOG_NOTICE:
      messages_id = NOTICE_LOG;
      break;

    case LOG_INFO:
      messages_id = INFO_LOG;
      break;

    default:
      messages_id = DEBUG_LOG;
      break;
    }

    message_p = itti_alloc_new_message_sized(origin_task_id, messages_id, message_string_size);

    switch (level) {
    case LOG_EMERG:
    case LOG_ALERT:
    case LOG_CRIT:
    case LOG_ERR:
      message_msg_p = (char *) &message_p->ittiMsg.error_log;
      break;

    case LOG_WARNING:
      message_msg_p = (char *) &message_p->ittiMsg.warning_log;
      break;

    case LOG_NOTICE:
      message_msg_p = (char *) &message_p->ittiMsg.notice_log;
      break;

    case LOG_INFO:
      message_msg_p = (char *) &message_p->ittiMsg.info_log;
      break;

    default:
      message_msg_p = (char *) &message_p->ittiMsg.debug_log;
      break;
    }

    memcpy(message_msg_p, log_start, message_string_size);

    itti_send_msg_to_task(TASK_UNKNOWN, INSTANCE_DEFAULT, message_p);
  }

#endif
#if 0
  LOG_params log_params;
  int        len;

  len = vsnprintf(log_params.l_buff_info, MAX_LOG_INFO-1, format, args);

  //2 first parameters must be passed as 'const' to the thread function
  log_params.file = strdup(file);
  log_params.func = strdup(func);
  log_params.line = line;
  log_params.comp = PHY;//comp;
  log_params.level = 6; // INFO - level;
  log_params.format = format;
  log_params.len = len;

  if (pthread_mutex_lock(&log_lock) != 0) {
    return;
  }

  log_list_tail++;
  log_list[log_list_tail - 1] = log_params;

  if (log_list_tail >= 1000) {
    log_list_tail = 0;
  }

  if (log_list_nb_elements < 1000) {
    log_list_nb_elements++;
  }

  if(pthread_cond_signal(&log_notify) != 0) {
    pthread_mutex_unlock(&log_lock);
    return;
  }

  if(pthread_mutex_unlock(&log_lock) != 0) {
    return;
  }

#endif
}

830 831
//log record: add to a list
void logRecord(const char *file, const char *func, int line,  int comp,
832
               int level, const char *format, ...)
833
{
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
  va_list    args;
  LOG_params log_params;
  int        len;

  va_start(args, format);
  len = vsnprintf(log_params.l_buff_info, MAX_LOG_INFO-1, format, args);
  va_end(args);

  //2 first parameters must be passed as 'const' to the thread function
  log_params.file = strdup(file);
  log_params.func = strdup(func);
  log_params.line = line;
  log_params.comp = comp;
  log_params.level = level;
  log_params.format = format;
  log_params.len = len;

  if (pthread_mutex_lock(&log_lock) != 0) {
    return;
  }
854

855 856
  log_list_tail++;
  log_list[log_list_tail - 1] = log_params;
857

858 859 860
  if (log_list_tail >= 1000) {
    log_list_tail = 0;
  }
861

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
  if (log_list_nb_elements < 1000) {
    log_list_nb_elements++;
  }

  if(pthread_cond_signal(&log_notify) != 0) {
    pthread_mutex_unlock(&log_lock);
    return;
  }

  if(pthread_mutex_unlock(&log_lock) != 0) {
    return;
  }

  //log = malloc(sizeof(LOG_elt));
  //log->next = NULL;
  //log->log_params = log_params;
  /* Add log task to queue */
  //log_list_add_tail_eurecom(log, &log_list);
880 881 882 883 884

}

void logRecord_thread_safe(const char *file, const char *func,
                           int line,  int comp, int level,
885 886
                           int len, const char *params_string)
{
887 888 889 890 891 892 893 894 895 896 897 898 899 900
  log_component_t *c;
  int total_len = 0;
  char log_buffer[MAX_LOG_TOTAL];

  c = &g_log->log_component[comp];

  // do not apply filtering for LOG_F
  // only log messages which are enabled and are below the global log level and component's level threshold
  if ((level != LOG_FILE) && ((c->level > g_log->level) ||
                              (level > c->level) || (level > g_log->level))) {
    return;
  }


901
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_LOG_RECORD,
902 903 904 905 906 907
                                          VCD_FUNCTION_IN);

  // adjust syslog level for TRACE messages
  if (g_log->syslog) {
    if (g_log->level > LOG_DEBUG) {
      g_log->level = LOG_DEBUG;
908
    }
909
  }
910

911 912 913 914 915 916 917 918 919
  // make sure that for log trace the extra info is only printed once, reset when the level changes
  if ((level == LOG_FILE) ||  (c->flag == LOG_NONE)  || (level ==LOG_TRACE )) {
    total_len = snprintf(&log_buffer[total_len], MAX_LOG_TOTAL - 1, "%s",
                         params_string);
  } else {
    if ((g_log->flag & FLAG_COLOR) || (c->flag & FLAG_COLOR)) {
      total_len += snprintf(&log_buffer[total_len], MAX_LOG_TOTAL - total_len, "%s",
                            log_level_highlight_start[level]);
    }
Lionel Gauthier's avatar
Lionel Gauthier committed
920

921 922 923
    if ((g_log->flag & FLAG_COMP) || (c->flag & FLAG_COMP) ) {
      total_len += snprintf(&log_buffer[total_len], MAX_LOG_TOTAL - total_len, "[%s]",
                            g_log->log_component[comp].name);
924
    }
925

926 927 928
    if ((g_log->flag & FLAG_LEVEL) || (c->flag & FLAG_LEVEL)) {
      total_len += snprintf(&log_buffer[total_len], MAX_LOG_TOTAL - total_len, "[%s]",
                            g_log->level2string[level]);
929
    }
930

931 932 933
    if ((g_log->flag & FLAG_FUNCT) || (c->flag & FLAG_FUNCT)) {
      total_len += snprintf(&log_buffer[total_len], MAX_LOG_TOTAL - total_len, "[%s] ",
                            func);
934 935
    }

936 937 938
    if ((g_log->flag & FLAG_FILE_LINE) || (c->flag & FLAG_FILE_LINE) )  {
      total_len += snprintf(&log_buffer[total_len], MAX_LOG_TOTAL - total_len, "[%s:%d]",
                            file, line);
939
    }
940

941 942 943 944 945 946
    len += snprintf(&log_buffer[total_len], MAX_LOG_TOTAL - len, "%s",
                    params_string);

    if ((g_log->flag & FLAG_COLOR) || (c->flag & FLAG_COLOR)) {
      total_len += snprintf(&log_buffer[total_len], MAX_LOG_TOTAL - total_len, "%s",
                            log_level_highlight_end[level]);
947
    }
948
  }
949

950 951 952 953
  // OAI printf compatibility
  if ((g_log->onlinelog == 1) && (level != LOG_FILE)) {
    fprintf(stdout, "%s", log_buffer);
  }
954

955 956 957 958 959 960 961
  if (g_log->syslog) {
    syslog(g_log->level, "%s", log_buffer);
  }

  if (g_log->filelog) {
    if (write(gfd, log_buffer, total_len) < total_len) {
      // TODO assert ?
962
    }
963 964 965 966 967
  }

  if ((g_log->log_component[comp].filelog) && (level == LOG_FILE)) {
    if (write(g_log->log_component[comp].fd, log_buffer, total_len) < total_len) {
      // TODO assert ?
968
    }
969 970
  }

971
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_LOG_RECORD,
972
                                          VCD_FUNCTION_OUT);
973 974
}

975 976 977
#if !defined(LOG_NO_THREAD)
void *log_thread_function(void *list)
{
978

979
  LOG_params log_params;
980

981
  for(;;) {
982

983
    //log_elt = NULL;
984

985 986
    /* Lock must be taken to wait on conditional variable */
    pthread_mutex_lock(&log_lock);
987

988 989
    /* Wait on condition variable, check for spurious wakeups.
       When returning from pthread_cond_wait(), we own the lock. */
990

991 992 993 994
    // sleep
    while((log_list_nb_elements == 0) && (log_shutdown == 0)) {
      pthread_cond_wait(&log_notify, &log_lock);
    }
995

996 997 998 999
    // exit
    if ((log_shutdown==1) && (log_list_nb_elements == 0)) {
      break;
    }
1000

1001 1002 1003 1004 1005
    /* Grab our task */
    //log_elt = log_list_remove_head(&log_list);
    log_params = log_list[log_list_head];
    log_list_head++;
    log_list_nb_elements--;
1006

1007 1008 1009
    if (log_list_head >= 1000) {
      log_list_head = 0;
    }
1010 1011


1012 1013
    /* Unlock */
    pthread_mutex_unlock(&log_lock);
1014

1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
    /* Get to work */
    logRecord_thread_safe(log_params.file,
                          log_params.func,
                          log_params.line,
                          log_params.comp,
                          log_params.level,
                          log_params.len,
                          log_params.l_buff_info);

    //free(log_elt);
  }
1026 1027
}
#endif
1028

1029 1030 1031 1032 1033 1034
#if 0
/* This function does not work. When multiple threads call it at the same time
 * for the same component, both threads end up using the same buffer.
 * The output is the completely messed up/wrong.
 * Kept in case the fix below is not correct or acceptable.
 */
1035 1036
//log record, format, and print:  executed in the main thread (mt)
void logRecord_mt(const char *file, const char *func, int line, int comp,
1037
                  int level, const char *format, ...)
1038
{
1039 1040 1041 1042 1043 1044
  int len = 0;
  va_list args;
  log_component_t *c;
  char *log_start;
  char *log_end;

Cedric Roux's avatar
Cedric Roux committed
1045 1046 1047 1048
  /* for no gcc warnings */
  (void)log_start;
  (void)log_end;

1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
  c = &g_log->log_component[comp];

  // do not apply filtering for LOG_F
  // only log messages which are enabled and are below the global log level and component's level threshold
  if ((level != LOG_FILE) && ((level > c->level) || (level > g_log->level))) {
    /* if ((level != LOG_FILE) &&
          ((level > c->level) ||
           (level > g_log->level) ||
           ( c->level > g_log->level))) {
    */
    return;
  }
1061

1062
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_LOG_RECORD,
1063
                                          VCD_FUNCTION_IN);
1064

1065
  va_start(args, format);
1066

1067 1068 1069 1070
  // adjust syslog level for TRACE messages
  if (g_log->syslog) {
    if (g_log->level > LOG_DEBUG) {
      g_log->level = LOG_DEBUG;
1071
    }
1072
  }
1073

1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
  // make sure that for log trace the extra info is only printed once, reset when the level changes
  if ((level == LOG_FILE) || (c->flag == LOG_NONE) || (level == LOG_TRACE)) {
    log_start = c->log_buffer;
    len = vsnprintf(c->log_buffer, MAX_LOG_TOTAL-1, format, args);
    log_end = c->log_buffer + len;
  } else {
    if ( (g_log->flag & FLAG_COLOR) || (c->flag & FLAG_COLOR) ) {
      len += snprintf(&c->log_buffer[len], MAX_LOG_TOTAL - len, "%s",
                      log_level_highlight_start[level]);
    }
1084

1085
    log_start = c->log_buffer + len;
1086

1087 1088 1089 1090
    if ( (g_log->flag & FLAG_COMP) || (c->flag & FLAG_COMP) ) {
      len += snprintf(&c->log_buffer[len], MAX_LOG_TOTAL - len, "[%s]",
                      g_log->log_component[comp].name);
    }
1091

1092 1093 1094 1095
    if ( (g_log->flag & FLAG_LEVEL) || (c->flag & FLAG_LEVEL) ) {
      len += snprintf(&c->log_buffer[len], MAX_LOG_TOTAL - len, "[%s]",
                      g_log->level2string[level]);
    }
1096

1097 1098 1099 1100 1101 1102 1103 1104 1105
    if ( (g_log->flag & FLAG_FUNCT) || (c->flag & FLAG_FUNCT) ) {
      len += snprintf(&c->log_buffer[len], MAX_LOG_TOTAL - len, "[%s] ",
                      func);
    }

    if ( (g_log->flag & FLAG_FILE_LINE) || (c->flag & FLAG_FILE_LINE) ) {
      len += snprintf(&c->log_buffer[len], MAX_LOG_TOTAL - len, "[%s:%d]",
                      file, line);
    }
1106

1107 1108
    len += vsnprintf(&c->log_buffer[len], MAX_LOG_TOTAL - len, format, args);
    log_end = c->log_buffer + len;
1109

1110 1111 1112
    if ( (g_log->flag & FLAG_COLOR) || (c->flag & FLAG_COLOR) ) {
      len += snprintf(&c->log_buffer[len], MAX_LOG_TOTAL - len, "%s",
                      log_level_highlight_end[level]);
1113
    }
1114
  }
1115

1116
  va_end(args);
1117

1118 1119
  // OAI printf compatibility
  if ((g_log->onlinelog == 1) && (level != LOG_FILE))
1120
    fwrite(c->log_buffer, len, 1, stdout);
1121 1122 1123 1124 1125 1126 1127 1128

  if (g_log->syslog) {
    syslog(g_log->level, "%s", c->log_buffer);
  }

  if (g_log->filelog) {
    if (write(gfd, c->log_buffer, len) < len) {
      // TODO assert ?
1129
    }
1130 1131 1132 1133 1134
  }

  if ((g_log->log_component[comp].filelog) && (level == LOG_FILE)) {
    if (write(g_log->log_component[comp].fd, c->log_buffer, len) < len) {
      // TODO assert ?
1135
    }
1136 1137
  }

1138 1139
#if defined(ENABLE_ITTI)

1140 1141 1142 1143 1144 1145 1146 1147
  if (level <= LOG_DEBUG) {
    task_id_t origin_task_id = TASK_UNKNOWN;
    MessagesIds messages_id;
    MessageDef *message_p;
    size_t      message_string_size;
    char       *message_msg_p;

    message_string_size = log_end - log_start;
1148

1149
#if !defined(DISABLE_ITTI_DETECT_SUB_TASK_ID)
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

    /* Try to identify sub task ID from log information (comp, log_instance_type) */
    switch (comp) {
    case PHY:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_PHY_ENB;
        break;

      case LOG_INSTANCE_UE:
        origin_task_id = TASK_PHY_UE;
        break;

      default:
        break;
      }

      break;

    case MAC:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_MAC_ENB;
        break;

      case LOG_INSTANCE_UE:
        origin_task_id = TASK_MAC_UE;

      default:
        break;
      }

      break;

    case RLC:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_RLC_ENB;
        break;

      case LOG_INSTANCE_UE:
        origin_task_id = TASK_RLC_UE;

      default:
        break;
      }

      break;

    case PDCP:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_PDCP_ENB;
        break;

      case LOG_INSTANCE_UE:
        origin_task_id = TASK_PDCP_UE;

      default:
        break;
      }

      break;

    default:
      break;
    }

1218 1219
#endif

1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
    switch (level) {
    case LOG_EMERG:
    case LOG_ALERT:
    case LOG_CRIT:
    case LOG_ERR:
      messages_id = ERROR_LOG;
      break;

    case LOG_WARNING:
      messages_id = WARNING_LOG;
      break;

    case LOG_NOTICE:
      messages_id = NOTICE_LOG;
      break;

    case LOG_INFO:
      messages_id = INFO_LOG;
      break;

    default:
      messages_id = DEBUG_LOG;
      break;
1243
    }
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

    message_p = itti_alloc_new_message_sized(origin_task_id, messages_id, message_string_size);

    switch (level) {
    case LOG_EMERG:
    case LOG_ALERT:
    case LOG_CRIT:
    case LOG_ERR:
      message_msg_p = (char *) &message_p->ittiMsg.error_log;
      break;

    case LOG_WARNING:
      message_msg_p = (char *) &message_p->ittiMsg.warning_log;
      break;

    case LOG_NOTICE:
      message_msg_p = (char *) &message_p->ittiMsg.notice_log;
      break;

    case LOG_INFO:
      message_msg_p = (char *) &message_p->ittiMsg.info_log;
      break;

    default:
      message_msg_p = (char *) &message_p->ittiMsg.debug_log;
      break;
    }

    memcpy(message_msg_p, log_start, message_string_size);

    itti_send_msg_to_task(TASK_UNKNOWN, INSTANCE_DEFAULT, message_p);
  }

1277 1278
#endif

1279 1280
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_LOG_RECORD,
                                          VCD_FUNCTION_OUT);
1281
}
1282
#endif /* #if 0 */
1283

1284 1285 1286
//log record, format, and print:  executed in the main thread (mt)
void logRecord_mt(const char *file, const char *func, int line, int comp,
                  int level, const char *format, ...)
1287
{
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
  int len = 0;
  va_list args;
  log_component_t *c;
  char *log_start;
  char *log_end;
  /* The main difference with the version above is the use of this local log_buffer.
   * The other difference is the return value of snprintf which was not used
   * correctly. It was not a big problem because in practice MAX_LOG_TOTAL is
   * big enough so that the buffer is never full.
   */
  char log_buffer[MAX_LOG_TOTAL];

Cedric Roux's avatar
Cedric Roux committed
1300 1301 1302 1303
  /* for no gcc warnings */
  (void)log_start;
  (void)log_end;

1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
  c = &g_log->log_component[comp];

  // do not apply filtering for LOG_F
  // only log messages which are enabled and are below the global log level and component's level threshold
  if ((level != LOG_FILE) && ((level > c->level) || (level > g_log->level))) {
    /* if ((level != LOG_FILE) &&
          ((level > c->level) ||
           (level > g_log->level) ||
           ( c->level > g_log->level))) {
    */
    return;
  }

  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_LOG_RECORD,
                                          VCD_FUNCTION_IN);

  va_start(args, format);

  // adjust syslog level for TRACE messages
  if (g_log->syslog) {
    if (g_log->level > LOG_DEBUG) {
      g_log->level = LOG_DEBUG;
    }
  }

  // make sure that for log trace the extra info is only printed once, reset when the level changes
  if ((level == LOG_FILE) || (c->flag == LOG_NONE) || (level == LOG_TRACE)) {
    log_start = log_buffer;
    len = vsnprintf(log_buffer, MAX_LOG_TOTAL, format, args);
    if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    log_end = log_buffer + len;
  } else {
    if ( (g_log->flag & FLAG_COLOR) || (c->flag & FLAG_COLOR) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "%s",
                      log_level_highlight_start[level]);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }

    log_start = log_buffer + len;

    if ( (g_log->flag & FLAG_COMP) || (c->flag & FLAG_COMP) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "[%s]",
                      g_log->log_component[comp].name);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }

    if ( (g_log->flag & FLAG_LEVEL) || (c->flag & FLAG_LEVEL) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "[%s]",
                      g_log->level2string[level]);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }

1356
    if ( (g_log->flag & FLAG_THREAD) || (c->flag & FLAG_THREAD) ) {
1357 1358
#     define THREAD_NAME_LEN 128
      char threadname[THREAD_NAME_LEN];
1359 1360 1361 1362 1363 1364 1365
      if (pthread_getname_np(pthread_self(), threadname, THREAD_NAME_LEN) != 0)
      {
        perror("pthread_getname_np : ");
      } else {
        len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "[%s]", threadname);
        if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
      }
1366
#     undef THREAD_NAME_LEN
1367 1368
    }

1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
    if ( (g_log->flag & FLAG_FUNCT) || (c->flag & FLAG_FUNCT) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "[%s] ",
                      func);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }

    if ( (g_log->flag & FLAG_FILE_LINE) || (c->flag & FLAG_FILE_LINE) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "[%s:%d]",
                      file, line);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }

    len += vsnprintf(&log_buffer[len], MAX_LOG_TOTAL - len, format, args);
    if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    log_end = log_buffer + len;

    if ( (g_log->flag & FLAG_COLOR) || (c->flag & FLAG_COLOR) ) {
      len += snprintf(&log_buffer[len], MAX_LOG_TOTAL - len, "%s",
                      log_level_highlight_end[level]);
      if (len > MAX_LOG_TOTAL) len = MAX_LOG_TOTAL;
    }
  }

  va_end(args);

  // OAI printf compatibility
  if ((g_log->onlinelog == 1) && (level != LOG_FILE))
    fwrite(log_buffer, len, 1, stdout);

  if (g_log->syslog) {
    syslog(g_log->level, "%s", log_buffer);
  }

  if (g_log->filelog) {
    if (write(gfd, log_buffer, len) < len) {
      // TODO assert ?
    }
  }

  if ((g_log->log_component[comp].filelog) && (level == LOG_FILE)) {
    if (write(g_log->log_component[comp].fd, log_buffer, len) < len) {
      // TODO assert ?
    }
  }

1414 1415
#if defined(ENABLE_ITTI)

1416 1417 1418 1419 1420 1421 1422 1423
  if (level <= LOG_DEBUG) {
    task_id_t origin_task_id = TASK_UNKNOWN;
    MessagesIds messages_id;
    MessageDef *message_p;
    size_t      message_string_size;
    char       *message_msg_p;

    message_string_size = log_end - log_start;
1424

1425
#if !defined(DISABLE_ITTI_DETECT_SUB_TASK_ID)
1426 1427 1428 1429 1430 1431 1432 1433

    /* Try to identify sub task ID from log information (comp, log_instance_type) */
    switch (comp) {
    case PHY:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_PHY_ENB;
        break;
1434

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
      case LOG_INSTANCE_UE:
        origin_task_id = TASK_PHY_UE;
        break;

      default:
        break;
      }

      break;

    case MAC:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_MAC_ENB;
        break;

      case LOG_INSTANCE_UE:
        origin_task_id = TASK_MAC_UE;

      default:
        break;
      }

      break;

    case RLC:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_RLC_ENB;
        break;

      case LOG_INSTANCE_UE:
        origin_task_id = TASK_RLC_UE;

      default:
        break;
      }

      break;

    case PDCP:
      switch (log_instance_type) {
      case LOG_INSTANCE_ENB:
        origin_task_id = TASK_PDCP_ENB;
        break;

      case LOG_INSTANCE_UE:
        origin_task_id = TASK_PDCP_UE;

      default:
        break;
      }

      break;

    default:
      break;
    }

1494 1495
#endif

1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
    switch (level) {
    case LOG_EMERG:
    case LOG_ALERT:
    case LOG_CRIT:
    case LOG_ERR:
      messages_id = ERROR_LOG;
      break;

    case LOG_WARNING:
      messages_id = WARNING_LOG;
      break;

    case LOG_NOTICE:
      messages_id = NOTICE_LOG;
      break;

    case LOG_INFO:
      messages_id = INFO_LOG;
      break;

    default:
      messages_id = DEBUG_LOG;
      break;
1519
    }
1520 1521

    message_p = itti_alloc_new_message_sized(origin_task_id, messages_id, message_string_size);
1522 1523

    switch (level) {
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
    case LOG_EMERG:
    case LOG_ALERT:
    case LOG_CRIT:
    case LOG_ERR:
      message_msg_p = (char *) &message_p->ittiMsg.error_log;
      break;

    case LOG_WARNING:
      message_msg_p = (char *) &message_p->ittiMsg.warning_log;
      break;

    case LOG_NOTICE:
      message_msg_p = (char *) &message_p->ittiMsg.notice_log;
      break;

    case LOG_INFO:
      message_msg_p = (char *) &message_p->ittiMsg.info_log;
      break;

    default:
      message_msg_p = (char *) &message_p->ittiMsg.debug_log;
      break;
1546
    }
1547

1548 1549 1550 1551 1552
    memcpy(message_msg_p, log_start, message_string_size);

    itti_send_msg_to_task(TASK_UNKNOWN, INSTANCE_DEFAULT, message_p);
  }

1553 1554
#endif

1555
  VCD_SIGNAL_DUMPER_DUMP_FUNCTION_BY_NAME(VCD_SIGNAL_DUMPER_FUNCTIONS_LOG_RECORD,
1556
                                          VCD_FUNCTION_OUT);
1557
}
1558 1559 1560

int set_log(int component, int level, int interval)
{
1561 1562 1563 1564 1565
  /* Checking parameters */
  DevCheck((component >= MIN_LOG_COMPONENTS) && (component < MAX_LOG_COMPONENTS),
           component, MIN_LOG_COMPONENTS, MAX_LOG_COMPONENTS);
  DevCheck((level <= LOG_TRACE) && (level >= LOG_EMERG), level, LOG_TRACE,
           LOG_EMERG);
1566
  DevCheck((interval >= 0) && (interval <= 0xFF), interval, 0, 0xFF);
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586

  g_log->log_component[component].level = level;

  switch (level) {
  case LOG_TRACE:
    g_log->log_component[component].flag = LOG_MED ;
    break;

  case LOG_DEBUG:
    g_log->log_component[component].flag = LOG_MED ;
    break;

  case LOG_INFO:
    g_log->log_component[component].flag = LOG_LOW ;
    break;

  default:
    g_log->log_component[component].flag = LOG_NONE ;
    break;
  }
1587

1588
  g_log->log_component[component].interval = interval;
1589

1590
  return 0;
1591 1592
}

1593 1594
int set_comp_log(int component, int level, int verbosity, int interval)
{
1595 1596 1597 1598 1599
  /* Checking parameters */
  DevCheck((component >= MIN_LOG_COMPONENTS) && (component < MAX_LOG_COMPONENTS),
           component, MIN_LOG_COMPONENTS, MAX_LOG_COMPONENTS);
  DevCheck((level <= LOG_TRACE) && (level >= LOG_EMERG), level, LOG_TRACE,
           LOG_EMERG);
1600
  DevCheck((interval >= 0) && (interval <= 0xFF), interval, 0, 0xFF);
1601

1602
#if 0
1603 1604 1605 1606 1607
  if ((verbosity == LOG_NONE) || (verbosity == LOG_LOW) ||
      (verbosity == LOG_MED) || (verbosity == LOG_FULL) ||
      (verbosity == LOG_HIGH)) {
    g_log->log_component[component].flag = verbosity;
  }
1608 1609 1610
#else
  g_log->log_component[component].flag = verbosity;
#endif
1611

1612 1613
  g_log->log_component[component].level = level;
  g_log->log_component[component].interval = interval;
1614

1615
  return 0;
1616 1617
}

1618 1619
void set_glog(int level, int verbosity)
{
1620 1621
  if( g_log->level >= 0) g_log->level = level;
  if( g_log->flag >= 0)  g_log->flag = verbosity;
1622
}
1623 1624
void set_glog_syslog(int enable)
{
1625
  g_log->syslog = enable;
1626
}
1627 1628
void set_glog_onlinelog(int enable)
{
1629
  g_log->onlinelog = enable;
1630
}
1631 1632
void set_glog_filelog(int enable)
{
1633
  g_log->filelog = enable;
1634 1635
}

1636 1637
void set_component_filelog(int comp)
{
1638 1639
  if (g_log->log_component[comp].filelog ==  0) {
    g_log->log_component[comp].filelog =  1;
1640

1641 1642
    if (g_log->log_component[comp].fd == 0) {
      g_log->log_component[comp].fd = open(g_log->log_component[comp].filelog_name,
1643
                                           O_WRONLY | O_CREAT | O_TRUNC, 0666);
1644
    }
1645
  }
1646 1647 1648 1649 1650 1651 1652
}

/*
 * 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 */
1653 1654
int map_str_to_int(mapping *map, const char *str)
{
1655 1656 1657 1658 1659 1660 1661
  while (1) {
    if (map->name == NULL) {
      return(-1);
    }

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

    map++;
  }
1666 1667 1668
}

/* map an int to a string. Takes a mapping array and a value */
1669 1670
char *map_int_to_str(mapping *map, int val)
{
1671 1672 1673
  while (1) {
    if (map->name == NULL) {
      return NULL;
1674
    }
1675 1676 1677

    if (map->value == val) {
      return map->name;
1678
    }
1679 1680 1681

    map++;
  }
1682
}
1683 1684 1685

int is_newline( char *str, int size)
{
1686 1687 1688 1689 1690
  int i;

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

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

void logClean (void)
{
1700
  int i;
1701

1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
  if (g_log->syslog) {
    closelog();
  }

  if (g_log->filelog) {
    close(gfd);
  }

  for (i=MIN_LOG_COMPONENTS; i < MAX_LOG_COMPONENTS; i++) {
    if (g_log->log_component[i].filelog) {
      close(g_log->log_component[i].fd);
1713
    }
1714
  }
1715 1716
}

1717 1718 1719
#if defined(ENABLE_ITTI)
void log_set_instance_type (log_instance_type_t instance)
{
1720
  log_instance_type = instance;
1721 1722 1723
}
#endif

1724 1725
#ifdef LOG_TEST

1726 1727
int main(int argc, char *argv[])
{
1728

1729
  logInit();
1730

1731 1732
  //set_log_syslog(1);
  test_log();
1733

1734
  return 1;
1735 1736
}

1737 1738
int test_log(void)
{
1739
  LOG_ENTER(MAC); // because the default level is DEBUG
1740
  LOG_I(SIM, "1 Starting OAI logs version %s Build date: %s on %s\n",
1741 1742 1743 1744 1745
        BUILD_VERSION, BUILD_DATE, BUILD_HOST);
  LOG_D(MAC, "1 debug  MAC \n");
  LOG_N(MAC, "1 notice MAC \n");
  LOG_W(MAC, "1 warning MAC \n");

1746
  set_comp_log(SIM, LOG_INFO, FLAG_ONLINE);
1747 1748
  set_comp_log(MAC, LOG_WARNING, 0);

1749
  LOG_I(SIM, "2 Starting OAI logs version %s Build date: %s on %s\n",
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
        BUILD_VERSION, BUILD_DATE, BUILD_HOST);
  LOG_E(MAC, "2 emerge MAC\n");
  LOG_D(MAC, "2 debug  MAC \n");
  LOG_N(MAC, "2 notice MAC \n");
  LOG_W(MAC, "2 warning MAC \n");
  LOG_I(MAC, "2 info MAC \n");


  set_comp_log(MAC, LOG_NOTICE, 1);

  LOG_ENTER(MAC);
1761
  LOG_I(SIM, "3 Starting OAI logs version %s Build date: %s on %s\n",
1762 1763 1764 1765 1766 1767 1768
        BUILD_VERSION, BUILD_DATE, BUILD_HOST);
  LOG_D(MAC, "3 debug  MAC \n");
  LOG_N(MAC, "3 notice MAC \n");
  LOG_W(MAC, "3 warning MAC \n");
  LOG_I(MAC, "3 info MAC \n");

  set_comp_log(MAC, LOG_DEBUG,1);
1769
  set_comp_log(SIM, LOG_DEBUG,1);
1770 1771

  LOG_ENTER(MAC);
1772
  LOG_I(SIM, "4 Starting OAI logs version %s Build date: %s on %s\n",
1773 1774 1775 1776 1777 1778 1779 1780
        BUILD_VERSION, BUILD_DATE, BUILD_HOST);
  LOG_D(MAC, "4 debug  MAC \n");
  LOG_N(MAC, "4 notice MAC \n");
  LOG_W(MAC, "4 warning MAC \n");
  LOG_I(MAC, "4 info MAC \n");


  set_comp_log(MAC, LOG_DEBUG,0);
1781
  set_comp_log(SIM, LOG_DEBUG,0);
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791

  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_N(MAC, "5 notice MAC \n");
  LOG_W(MAC, "5 warning MAC \n");
  LOG_I(MAC, "5 info MAC \n");


  set_comp_log(MAC, LOG_TRACE,0X07F);
1792
  set_comp_log(SIM, LOG_TRACE,0X07F);
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803

  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_N(MAC, "6 notice MAC \n");
  LOG_W(MAC, "6 warning MAC \n");
  LOG_I(MAC, "6 info MAC \n");
  LOG_EXIT(MAC);

  return 0;
1804 1805
}
#endif