Back to home page

Enduro/X

 
 

    


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