Hello! At the institution I work for, we use programs in Pro*C (SQL embebbed in C language)
There are multiple servers in the architecture, and problems can occur when trying to know where to look for the log file if there is an error. I am trying to sort the log files because it is like a mess
We should manage some log levels, like: info, warning, error and fatal, and log files should be saves in a directory: /interfaces/logs
These pieces of code use the library
<syslog.h>, and <syslog.c>:
In `ifcp0150.pc`:
void GrabacionLog(int p_nivel_mensaje,char *p_texto,int p_numero_linea,int p_tipo_mensaje){
if(p_tipo_mensaje == MENSAJELOG){
syslog(p_nivel_mensaje, "[%5d] ---- %s ", p_numero_linea, p_texto);
}
else{
syslog(p_nivel_mensaje, "[%5d] ---- %s [Error : %m]",p_numero_linea,p_texto);
}
}
void AperturaLog(char *p_nombre_programa, int p_facilidad){
openlog(p_nombre_programa, LOG_PID | LOG_NDELAY | LOG_NOWAIT , p_facilidad);
MensajeLogInformacion("---- Comienzo Ejecucion ----", __LINE__);
}
The function `GrabacionLog` has some alias, depending of the kind of log:
#define MENSAJELOG 0
#define ERRORLOG 1
#define MensajeLogEmergencia(y,z) GrabacionLog(LOG_EMERG,y,z,MENSAJELOG) /* 0 */
#define MensajeLogAlerta(y,z) GrabacionLog(LOG_ALERT,y,z,MENSAJELOG) /* 1 */
#define MensajeLogCritico(y,z) GrabacionLog(LOG_CRIT,y,z,MENSAJELOG) /* 2 */
#define MensajeLogError(y,z) GrabacionLog(LOG_ERR,y,z,MENSAJELOG) /* 3 */
#define MensajeLogAdvertencia(y,z) GrabacionLog(LOG_WARNING,y,z,MENSAJELOG) /* 4 */
#define MensajeLogNoticia(y,z) GrabacionLog(LOG_NOTICE,y,z,MENSAJELOG) /* 5 */
#define MensajeLogInformacion(y,z) GrabacionLog(LOG_INFO,y,z,MENSAJELOG) /* 6 */
#define MensajeLogDebug(y,z) GrabacionLog(LOG_DEBUG,y,z,MENSAJELOG) /* 7 */
#define ErrorLogEmergencia(y,z) GrabacionLog(LOG_EMERG,y,z,ERRORLOG) /* 0 */
#define ErrorLogAlerta(y,z) GrabacionLog(LOG_ALERT,y,z,ERRORLOG) /* 1 */
#define ErrorLogCritico(y,z) GrabacionLog(LOG_CRIT,y,z,ERRORLOG) /* 2 */
#define ErrorLogError(y,z) GrabacionLog(LOG_ERR,y,z,ERRORLOG) /* 3 */
#define ErrorLogAdvertencia(y,z) GrabacionLog(LOG_WARNING,y,z,ERRORLOG) /* 4 */
#define ErrorLogNoticia(y,z) GrabacionLog(LOG_NOTICE,y,z,ERRORLOG) /* 5 */
#define ErrorLogInformacion(y,z) GrabacionLog(LOG_INFO,y,z,ERRORLOG) /* 6 */
#define ErrorLogDebug(y,z) GrabacionLog(LOG_DEBUG,y,z,ERRORLOG) /* 7 */
#define AperturaLogDemonio(y) AperturaLog(y,LOG_LOCAL7)
#define AperturaLogReportes(y) AperturaLog(y,LOG_LOCAL6)
In `reportsdaemon.pc`:
int main(int argc, char *argv[]){
UseSysLogMessage(argv[0]);
UseSysLogDebug(argv[0]);
In `ifcp0170.c`:
UseSysLogMessage(argv[0]);
UseSysLogDebug(argv[0]);
SetDebugLevel(1);
In `ue_funcion.pc`:
UseSysLogMessage(funcion);
and afterwards:
UseSysLogMessage("ue_maquina_estados.pc");
There are also functions that do not use the syslog library and they just write on a file, like these ones:
void log_error_msg(const char *context_info, char *mensaje, ... ){
FILE *fp;
char arch_log[150];
va_list ap;
char desc[200];
char host[50];
varchar dia_hora[100];
long sql_code;
char l_directorio[100] = "";
char l_paso[100];
sql_code = sqlca.sqlcode;
va_start(ap, mensaje);
vsprintf(desc,mensaje, ap);
va_end(ap);
exec sql
select to_char(sysdate, 'Mon dd hh24:mi:ss')
into :dia_hora
from dual;
ora_close(dia_hora);
step_ind(desc);
if(getenv("DEBUG_LOG_ACR")!=NULL AND strcmp(getenv("DEBUG_LOG_ACR"),"S")==0){
PATH_MSG(l_directorio, l_paso);
strcpy(arch_log, l_paso);
strcat(arch_log, ARCHIVO_MSG);
fp=fopen(arch_log, "a");
fprintf(fp, (char *)dia_hora.arr);
fprintf(fp, " ");
if(getenv("SESSION_SVR") != NULL ){
strcpy(host, getenv("SESSION_SVR"));
fprintf(fp, host);
fprintf(fp, " ");
}
fprintf(fp, context_info);
fprintf(fp, " ");
fprintf(fp, desc);
fprintf(fp, " ");
fprintf(fp, "SQLCODE:%ld",sql_code);
fprintf(fp, "\n");
fclose(fp);
}
}
void log_error(char *mensaje, ... ){
FILE *fp;
char arch_log[150];
va_list ap;
char desc[200];
char l_directorio[100];
char l_paso[100];
va_start(ap, mensaje);
vsprintf(desc, mensaje, ap);
va_end(ap);
step_ind(desc);
if(getenv("DEBUG_LOG_ACR")!=NULL AND strcmp(getenv("DEBUG_LOG_ACR"),"S")==0){
PATH(l_directorio, l_paso);
strcpy(arch_log, l_paso);
strcat(arch_log, ARCHIVO);
fp= fopen(arch_log, "a");
fprintf(fp, desc);
fprintf(fp, "\n");
fclose(fp);
}
}
We would like to unify the logs and use just the `syslog`. Any advice/tip please?
What I have tried:
The log_error_msg() and log_error() functions will be revised to write the output to syslog()