Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief User log
0003  *
0004  * @file ulog.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2018, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * GPL or Mavimax's license for commercial use.
0012  * -----------------------------------------------------------------------------
0013  * GPL license:
0014  * 
0015  * This program is free software; you can redistribute it and/or modify it under
0016  * the terms of the GNU General Public License as published by the Free Software
0017  * Foundation; either version 3 of the License, or (at your option) any later
0018  * version.
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0023  *
0024  * You should have received a copy of the GNU General Public License along with
0025  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
0026  * Place, Suite 330, Boston, MA 02111-1307 USA
0027  *
0028  * -----------------------------------------------------------------------------
0029  * A commercial use license is available from Mavimax, Ltd
0030  * contact@mavimax.com
0031  * -----------------------------------------------------------------------------
0032  */
0033 #include <ndrx_config.h>
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <time.h>
0039 #include <sys/time.h>
0040 #include <unistd.h>
0041 #include <stdarg.h>
0042 
0043 #include <ndrstandard.h>
0044 #include <ndebug.h>
0045 #include <nstdutil.h>
0046 #include <sys_unix.h>
0047 #include <userlog.h>
0048 #include <atmi.h>
0049 /*---------------------------Externs------------------------------------*/
0050 /*---------------------------Macros-------------------------------------*/
0051 /*---------------------------Enums--------------------------------------*/
0052 /*---------------------------Typedefs-----------------------------------*/
0053 /*---------------------------Globals------------------------------------*/
0054 /*---------------------------Statics------------------------------------*/
0055 /*---------------------------Prototypes---------------------------------*/
0056 
0057 /**
0058  * API version of USERLOG
0059  */
0060 expublic int userlog (char *data, ...)
0061 {
0062     int ret=EXSUCCEED;
0063     /* TODO: Might need semaphore for first init... */
0064     static int first = 1;
0065     static char *out_f = NULL;
0066     static char *out_f_dflt = ".";
0067     FILE *output;
0068     char  pre[100];
0069     int fopened=0;
0070     struct timeval  time_val;
0071     char full_name[FILENAME_MAX] = {EXEOS};
0072     long ldate, ltime, lusec;
0073     int print_label = 0;
0074     pid_t pid;
0075     va_list ap;
0076     /* No need for contexting... */
0077 
0078     gettimeofday( &time_val, NULL );
0079     
0080     ndrx_get_dt_local(&ldate, &ltime, &lusec);
0081     
0082     if (first)
0083     {
0084         if (NULL==(out_f=getenv(CONF_NDRX_ULOG)))
0085         {
0086             print_label = 1;
0087             out_f=out_f_dflt;
0088         }
0089 
0090         /* get pid */
0091         pid = getpid();
0092         first = 0;
0093     }
0094 
0095     /* Format the full output file */
0096     if (NULL!=out_f)
0097     {
0098         snprintf(full_name, sizeof(full_name), "%s/ULOG.%06ld", out_f, ldate);
0099         
0100         if (print_label)
0101         {
0102             fprintf(stderr, "Logging to %s\n", full_name);
0103         }
0104     }
0105 
0106     /* if no file or failed to open, then use stderr as output */
0107     /* we cannot have fopen/fclose debug here, it will cause recursion */
0108     if (NULL==out_f || NULL==(output=fopen(full_name, "a")))
0109     {
0110         if (NULL!=out_f)
0111         {
0112             fprintf(stderr, "Failed to open [%s]\n", full_name);
0113         }
0114         output=stderr;
0115     }
0116     else
0117     {
0118         fopened=1;
0119     }
0120     
0121     snprintf(pre, sizeof(pre), "%5ld:%08ld:%06ld%02ld:%-12.12s:",
0122             (long)pid, ldate, ltime,
0123                     (long)time_val.tv_usec/10000, EX_PROGNAME);
0124 
0125     va_start(ap, data);
0126     fputs(pre, output);
0127     (void) vfprintf(output, data, ap);
0128     fputs("\n", output);
0129     va_end(ap);
0130 
0131     if (fopened)
0132     {
0133         fclose( output );
0134     }
0135     
0136 out:
0137     return ret;
0138 }
0139 
0140 /**
0141  * Write the userlog message by const string
0142  * @param msg
0143  */
0144 expublic int userlog_const (const char *msg)
0145 {
0146     return userlog("%s", msg);
0147 }
0148