/********************************************************************* * * Function : init_log_module * * Description : Initializes the logging module to log to stderr. * Can only be called while stderr hasn't been closed * yet and is only supposed to be called once. * * Parameters : * 1 : prog_name = The program name. * * Returns : Nothing. * *********************************************************************/ void init_log_module(void) { lock_logfile(); logfp = stderr; unlock_logfile(); set_debug_level(debug); }
/** * @function logfile.flush * * ### Synopsis * * logfile.flush(handle); * * Flush contents of shared memory block to the log file, reset memory block to "empty" state. * * @param {object} handle - handle of logfile to flush. */ static JSVAL logfile_flush (JSARGS args) { STATE *state = HANDLE(args[0]); lock_logfile(state); flush_logfile(state); unlock_logfile(state); return Undefined(); }
/** * @function logfile.flush * * ### Synopsis * * logfile.flush(handle); * * Flush contents of shared memory block to the log file, reset memory block to "empty" state. * * @param {object} handle - handle of logfile to flush. */ static JSVAL logfile_flush (JSARGS args) { HandleScope scope; STATE *state = log_HANDLE(args[0]); lock_logfile(state); flush_logfile(state); unlock_logfile(state); return Undefined(); }
/********************************************************************* * * Function : disable_logging * * Description : Disables logging. * * Parameters : None. * * Returns : Nothing. * *********************************************************************/ void disable_logging(void) { if (logfp != NULL) { log_error(LOG_LEVEL_INFO, "No logfile configured. Please enable it before reporting any problems."); lock_logfile(); fclose(logfp); logfp = NULL; unlock_logfile(); } }
/** * @function logfile.write * * ### Synopsis * * logfile.write(handle, s); * logfile.write(handle, s, len); * * Write a string to the log file. * * The string is appended to the shared memory block. The memory block is first flushed to disk if there is not enough room for the string. * * @param {object} handle - handle of the log file. * @param {string} s - string to write to the log file. * @param {int} len - optional length of string to write; defaults to strlen(s). * */ static JSVAL logfile_write (JSARGS args) { STATE *state = HANDLE(args[0]); String::AsciiValue buf(args[1]); int len; if (args.Length() > 2) { len = args[2]->IntegerValue(); } else { len = strlen(*buf); } lock_logfile(state); if (*state->length + len >= LOGFILE_CHUNK_SIZE) { flush_logfile(state); } memcpy(&state->logBuffer[*state->length], *buf, len); *state->length += len; unlock_logfile(state); return Undefined(); }
/********************************************************************* * * Function : init_error_log * * Description : Initializes the logging module to log to a file. * * XXX: should be renamed. * * Parameters : * 1 : prog_name = The program name. * 2 : logfname = The logfile to (re)open. * * Returns : N/A * *********************************************************************/ void init_error_log(const char *prog_name, const char *logfname) { FILE *fp; assert(NULL != logfname); lock_loginit(); if ((logfp != NULL) && (logfp != stderr)) { log_error(LOG_LEVEL_INFO, "(Re-)Opening logfile \'%s\'", logfname); } /* set the designated log file */ fp = fopen(logfname, "a"); if ((NULL == fp) && (logfp != NULL)) { /* * Some platforms (like OS/2) don't allow us to open * the same file twice, therefore we give it another * shot after closing the old file descriptor first. * * We don't do it right away because it prevents us * from logging the "can't open logfile" message to * the old logfile. * * XXX: this is a lame workaround and once the next * release is out we should stop bothering reopening * the logfile unless we have to. * * Currently we reopen it every time the config file * has been reloaded, but actually we only have to * reopen it if the file name changed or if the * configuration reload was caused by a SIGHUP. */ log_error(LOG_LEVEL_INFO, "Failed to reopen logfile: \'%s\'. " "Retrying after closing the old file descriptor first. If that " "doesn't work, Privoxy will exit without being able to log a message.", logfname); lock_logfile(); fclose(logfp); logfp = NULL; unlock_logfile(); fp = fopen(logfname, "a"); } if (NULL == fp) { log_error(LOG_LEVEL_FATAL, "init_error_log(): can't open logfile: \'%s\'", logfname); } /* set logging to be completely unbuffered */ setbuf(fp, NULL); lock_logfile(); if (logfp != NULL) { fclose(logfp); } #ifdef unix if (daemon_mode && (logfp == stderr)) { if (dup2(1, 2) == -1) { /* * We only use fatal_error() to clear the pid * file and to exit. Given that stderr has just * been closed, the user will not see the error * message. */ fatal_error("Failed to reserve fd 2."); } } #endif logfp = fp; unlock_logfile(); show_version(prog_name); unlock_loginit(); } /* init_error_log */