Beispiel #1
0
void pkcs11_logger_log(const char* message, ...)
{
	FILE *fw = NULL;
	FILE *output = stdout;
	va_list ap;
	
	// Acquire exclusive access to the file
	pkcs11_logger_lock_acquire();
	
	va_start(ap, message);
	
	// Determine log filename
	char *log_file_name = getenv("PKCS11_LOGGER_LOG_FILE");
	if (NULL == log_file_name)
		log_file_name = DEFAULT_PKCS11_LOGGER_LOG_FILE;
	
	// Open log file
	fw = fopen(log_file_name, "a");
	if (NULL != fw)
		output = fw;
	
	// Output message to log file or stdout
	fprintf(output, "%0#10x : ", get_process_id());
	fprintf(output, "%0#10x : ", get_thread_id());
	vfprintf(output, message, ap);
	fprintf(output, "\n");
	
	// Cleanup
	va_end(ap);
	CALL_N_CLEAR(fclose, fw);
	
	// Release exclusive access to the file
	pkcs11_logger_lock_release();
}
Beispiel #2
0
// Logs message
void pkcs11_logger_log(const char* message, ...)
{
    va_list ap;

    unsigned long disable_log_file = ((pkcs11_logger_globals.flags & PKCS11_LOGGER_FLAG_DISABLE_LOG_FILE) == PKCS11_LOGGER_FLAG_DISABLE_LOG_FILE);
    unsigned long disable_process_id = ((pkcs11_logger_globals.flags & PKCS11_LOGGER_FLAG_DISABLE_PROCESS_ID) == PKCS11_LOGGER_FLAG_DISABLE_PROCESS_ID);
    unsigned long disable_thread_id = ((pkcs11_logger_globals.flags & PKCS11_LOGGER_FLAG_DISABLE_THREAD_ID) == PKCS11_LOGGER_FLAG_DISABLE_THREAD_ID);
    unsigned long enable_stdout = ((pkcs11_logger_globals.flags & PKCS11_LOGGER_FLAG_ENABLE_STDOUT) == PKCS11_LOGGER_FLAG_ENABLE_STDOUT);
    unsigned long enable_stderr = ((pkcs11_logger_globals.flags & PKCS11_LOGGER_FLAG_ENABLE_STDERR) == PKCS11_LOGGER_FLAG_ENABLE_STDERR);
    unsigned long enable_fclose = ((pkcs11_logger_globals.flags & PKCS11_LOGGER_FLAG_ENABLE_FCLOSE) == PKCS11_LOGGER_FLAG_ENABLE_FCLOSE);

    // Acquire exclusive access to the file
    pkcs11_logger_lock_acquire();

#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable: 4996)
#endif

    // Open log file
    if ((!disable_log_file) && (NULL != pkcs11_logger_globals.env_var_log_file_path) && (NULL == pkcs11_logger_globals.log_file_handle))
    {
        pkcs11_logger_globals.log_file_handle = fopen((const char *)pkcs11_logger_globals.env_var_log_file_path, "a");
    }

#ifdef _WIN32
#pragma warning(pop)
#endif

    // Log to file
    if ((!disable_log_file) && (NULL != pkcs11_logger_globals.log_file_handle))
    {
        va_start(ap, message);

        if (!disable_process_id)
            fprintf(pkcs11_logger_globals.log_file_handle, "%0#10x : ", pkcs11_logger_utils_get_process_id());
        if (!disable_thread_id)
            fprintf(pkcs11_logger_globals.log_file_handle, "%0#10x : ", pkcs11_logger_utils_get_thread_id());
        vfprintf(pkcs11_logger_globals.log_file_handle, message, ap);
        fprintf(pkcs11_logger_globals.log_file_handle, "\n");

        va_end(ap);
    }

    // Log to stdout
    if (enable_stdout)
    {
        va_start(ap, message);

        if (!disable_process_id)
            fprintf(stdout, "%0#10x : ", pkcs11_logger_utils_get_process_id());
        if (!disable_thread_id)
            fprintf(stdout, "%0#10x : ", pkcs11_logger_utils_get_thread_id());
        vfprintf(stdout, message, ap);
        fprintf(stdout, "\n");

        va_end(ap);
    }

    // Log to stderr
    if (enable_stderr || CK_FALSE == pkcs11_logger_globals.env_vars_read)
    {
        va_start(ap, message);

        if (!disable_process_id)
            fprintf(stderr, "%0#10x : ", pkcs11_logger_utils_get_process_id());
        if (!disable_thread_id)
            fprintf(stderr, "%0#10x : ", pkcs11_logger_utils_get_thread_id());
        vfprintf(stderr, message, ap);
        fprintf(stderr, "\n");

        va_end(ap);
    }

    // Cleanup
    if (enable_fclose)
    {
        CALL_N_CLEAR(fclose, pkcs11_logger_globals.log_file_handle);
    }
    else
    {
        if (NULL != pkcs11_logger_globals.log_file_handle)
            fflush(pkcs11_logger_globals.log_file_handle);
    }
    
    // Release exclusive access to the file
    pkcs11_logger_lock_release();
}