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-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
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 Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <ndrx_config.h>
0035 #include <string.h>
0036 #include <stdio.h>
0037 #include <stdlib.h>
0038 #include <memory.h>
0039 #include <time.h>
0040 #include <sys/time.h>
0041 #include <unistd.h>
0042 #include <stdarg.h>
0043 
0044 #include <ndrstandard.h>
0045 #include <ndebug.h>
0046 #include <nstdutil.h>
0047 #include <sys_unix.h>
0048 #include <userlog.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 = getpid();
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         first = 0;
0091     }
0092 
0093     /* Format the full output file */
0094     if (NULL!=out_f)
0095     {
0096         snprintf(full_name, sizeof(full_name), "%s/ULOG.%06ld", out_f, ldate);
0097         
0098         if (print_label)
0099         {
0100             fprintf(stderr, "Logging to %s\n", full_name);
0101         }
0102     }
0103 
0104     /* if no file or failed to open, then use stderr as output */
0105     /* we cannot have fopen/fclose debug here, it will cause recursion */
0106     if (NULL==out_f || NULL==(output=fopen(full_name, "a")))
0107     {
0108         if (NULL!=out_f)
0109         {
0110             fprintf(stderr, "Failed to open [%s]\n", full_name);
0111         }
0112         output=stderr;
0113     }
0114     else
0115     {
0116         fopened=1;
0117     }
0118     
0119     snprintf(pre, sizeof(pre), "%05lu:%08ld:%06ld%02ld:%-12.12s:",
0120             (long)pid, ldate, ltime,
0121                     (long)time_val.tv_usec/10000, EX_PROGNAME);
0122 
0123     va_start(ap, data);
0124     fputs(pre, output);
0125     (void) vfprintf(output, data, ap);
0126     fputs("\n", output);
0127     va_end(ap);
0128 
0129     if (fopened)
0130     {
0131         fclose( output );
0132     }
0133     
0134 out:
0135     return ret;
0136 }
0137 
0138 /**
0139  * Write the userlog message by const string
0140  * @param msg
0141  */
0142 expublic int userlog_const (const char *msg)
0143 {
0144     return userlog("%s", msg);
0145 }
0146 
0147 /* vim: set ts=4 sw=4 et smartindent: */