irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

rsLog.cpp
Go to the documentation of this file.
1 
3 /* rsLog.c - Routines for log files.
4  */
5 
6 #include "rsLog.hpp"
7 #include "rsGlobalExtern.hpp"
8 
9 static time_t LogfileLastChkTime = 0;
10 
11 char *
13  char *myDir;
14 
15  if ( ( myDir = ( char * ) getenv( "irodsLogDir" ) ) != ( char * ) NULL ) {
16  return myDir;
17  }
18  return DEF_LOG_DIR;
19 }
20 
22  char* rotation_time_str = getenv(LOGFILE_INT);
23  if ( rotation_time_str ) {
24  const int rotation_time = atoi(rotation_time_str);
25  if( rotation_time >= 1 ) {
26  return rotation_time;
27  }
28  }
29 
30  try {
31  const int rotation_time = irods::get_advanced_setting<const int>(irods::DEFAULT_LOG_ROTATION_IN_DAYS);
32  if(rotation_time >= 1) {
33  return rotation_time;
34  }
35 
36  }
37  catch( irods::exception& _e ) {
38  }
39 
40  return DEF_LOGFILE_INT;
41 
42 } // get_log_file_rotation_time
43 
44 void
45 getLogfileName( char **logFile, const char *logDir, const char *logFileName ) {
46  time_t myTime;
47  struct tm *mytm = nullptr;
48  char *logfilePattern = nullptr; // JMC - backport 4793
49  int tm_mday = 1;
50  char logfileSuffix[MAX_NAME_LEN]; // JMC - backport 4793
51  char myLogDir[MAX_NAME_LEN];
52 
53  /* Put together the full pathname of the logFile */
54 
55  if ( logDir == NULL ) {
56  snprintf( myLogDir, MAX_NAME_LEN, "%-s", getLogDir() );
57  }
58  else {
59  snprintf( myLogDir, MAX_NAME_LEN, "%-s", logDir );
60  }
61  *logFile = ( char * ) malloc( strlen( myLogDir ) + strlen( logFileName ) + 24 );
62 
63 
64 
65 
66  LogfileLastChkTime = myTime = time( 0 );
67  mytm = localtime( &myTime );
68  const int rotation_time = get_log_file_rotation_time();
69 
70  tm_mday = ( mytm->tm_mday / rotation_time ) * rotation_time + 1;
71  if ( tm_mday > mytm->tm_mday ) {
72  tm_mday -= rotation_time;
73  }
74  // =-=-=-=-=-=-=-
75  // JMC - backport 4793
76  if ( ( logfilePattern = getenv( LOGFILE_PATTERN ) ) == NULL ) {
77  logfilePattern = DEF_LOGFILE_PATTERN;
78  }
79  mytm->tm_mday = tm_mday;
80  strftime( logfileSuffix, MAX_NAME_LEN, logfilePattern, mytm );
81  sprintf( *logFile, "%-s/%-s.%-s", myLogDir, logFileName, logfileSuffix );
82  // =-=-=-=-=-=-=-
83 
84 
85 }
86 
87 int
88 chkLogfileName( const char *logDir, const char *logFileName ) {
89  time_t myTime;
90  char *logFile = NULL;
91  int i;
92 
93  myTime = time( 0 );
94  if ( myTime < LogfileLastChkTime + LOGFILE_CHK_INT ) {
95  /* not time yet */
96  return 0;
97  }
98 
99  getLogfileName( &logFile, logDir, logFileName );
100 
101  if ( CurLogfileName != NULL && strcmp( CurLogfileName, logFile ) == 0 ) {
102  free( logFile );
103  return 0;
104  }
105 
106  /* open the logfile */
107 
108  if ( ( i = open( logFile, O_CREAT | O_RDWR, 0644 ) ) < 0 ) {
109  fprintf( stderr, "Unable to open logFile %s\n", logFile );
110  free( logFile );
111  return -1;
112  }
113  else {
114  lseek( i, 0, SEEK_END );
115  }
116 
117  if ( CurLogfileName != NULL ) {
118  free( CurLogfileName );
119  }
120 
121  CurLogfileName = logFile;
122 
123  close( 0 );
124  close( 1 );
125  close( 2 );
126  ( void ) dup2( i, 0 );
127  ( void ) dup2( i, 1 );
128  ( void ) dup2( i, 2 );
129  ( void ) close( i );
130 
131  return 0;
132 }
NULL
#define NULL
Definition: rodsDef.h:70
getLogfileName
void getLogfileName(char **logFile, const char *logDir, const char *logFileName)
Definition: rsLog.cpp:45
DEF_LOGFILE_INT
#define DEF_LOGFILE_INT
Definition: rsLog.hpp:20
LogfileLastChkTime
static time_t LogfileLastChkTime
Definition: rsLog.cpp:9
CurLogfileName
char * CurLogfileName
Definition: irods_server_globals.cpp:17
DEF_LOG_DIR
#define DEF_LOG_DIR
Definition: rsLog.hpp:16
rsLog.hpp
DEF_LOGFILE_PATTERN
#define DEF_LOGFILE_PATTERN
Definition: rsLog.hpp:26
LOGFILE_PATTERN
#define LOGFILE_PATTERN
Definition: rsLog.hpp:27
rsGlobalExtern.hpp
LOGFILE_INT
#define LOGFILE_INT
Definition: rsLog.hpp:21
get_log_file_rotation_time
int get_log_file_rotation_time()
Definition: rsLog.cpp:21
MAX_NAME_LEN
#define MAX_NAME_LEN
Definition: rodsDef.h:61
chkLogfileName
int chkLogfileName(const char *logDir, const char *logFileName)
Definition: rsLog.cpp:88
LOGFILE_CHK_INT
#define LOGFILE_CHK_INT
Definition: rsLog.hpp:24
irods::exception
Definition: irods_exception.hpp:15
irods::DEFAULT_LOG_ROTATION_IN_DAYS
const std::string DEFAULT_LOG_ROTATION_IN_DAYS("default_log_rotation_in_days")
getLogDir
char * getLogDir()
Definition: rsLog.cpp:12