/** * This is the simplest of tests to check we can log things without crashing. * * In fact, because of the way logging works (differently) in test mode than it does in "agent mode" * all we're really doing here is to test that logging in test mode isn't broken. This may or may not * bear any relation to whether logging works for the rest of the time. */ void test_logging_in_unit_test_mode(void** state) { static const char* text1 = "Now is the winter of our discontent,"; static const char* text2 = "Made glorious summer by this son of York"; AM_LOG_INFO(0, "instance id is zero and no args"); AM_LOG_INFO(0, "instance id is zero and incorrect args", text1); AM_LOG_INFO(0, "instance id is zero and more incorrect args", text1, text2); /* we're testing this will not crash */ AM_LOG_INFO(0, NULL, text1, text2); /* this will not appear, since the instance is greater than zero, but it should not crash either */ AM_LOG_ERROR(10, "%s %s", text1, text2); AM_LOG_INFO(0, "%s %s", text1, text2); AM_LOG_WARNING(0, "%s %s", text1, text2); AM_LOG_ERROR(0, "%s %s", text1, text2); AM_LOG_DEBUG(0, "%s %s", text1, text2); AM_LOG_AUDIT(0, "%s %s", text1, text2); AM_LOG_ALWAYS(0, "%s %s", text1, text2); AM_LOG_ALWAYS(0, "Now %s the %s of our %s, %s summ%s of York", "is", "winter", "discontent", "Made glorious", "er by this son"); /* attempt to overflow the buffer, although this will be ultimately unsuccessful because the * logging works differently in unit test mode than it does in "real life" mode. */ AM_LOG_ALWAYS(0, "\n" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" "ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJABCDEFGHIJ"); }
void am_log_register_instance(unsigned long instance_id, const char *debug_log, int log_level, int log_size, const char *audit_log, int audit_level, int audit_size) { int i, exist = AM_NOT_FOUND; struct am_log *log = AM_LOG(); struct log_files *f = NULL; if (log == NULL || instance_id == 0 || ISINVALID(debug_log) || ISINVALID(audit_log)) { return; } #ifdef _WIN32 WaitForSingleObject(am_log_lck.lock, INFINITE); #else pthread_mutex_lock(&log->lock); #endif for (i = 0; i < AM_MAX_INSTANCES; i++) { f = &log->files[i]; if (f->instance_id == instance_id) { exist = AM_SUCCESS; break; } } if (exist == AM_NOT_FOUND) { for (i = 0; i < AM_MAX_INSTANCES; i++) { f = &log->files[i]; if (!f->used) { f->instance_id = instance_id; snprintf(f->name_debug, sizeof (f->name_debug), "%s", debug_log); snprintf(f->name_audit, sizeof (f->name_audit), "%s", audit_log); f->used = AM_TRUE; #define DEFAULT_LOG_SIZE (1024 * 1024 * 5) /* 5MB */ f->max_size_debug = log_size > 0 && log_size < DEFAULT_LOG_SIZE ? DEFAULT_LOG_SIZE : log_size; f->max_size_audit = audit_size > 0 && audit_size < DEFAULT_LOG_SIZE ? DEFAULT_LOG_SIZE : audit_size; f->level_debug = log_level; f->level_audit = audit_level; f->created_debug = f->created_audit = 0; f->owner = 0; exist = AM_DONE; break; } } } else { /* update instance logging level configuration */ f->max_size_debug = log_size > 0 && log_size < DEFAULT_LOG_SIZE ? DEFAULT_LOG_SIZE : log_size; f->max_size_audit = audit_size > 0 && audit_size < DEFAULT_LOG_SIZE ? DEFAULT_LOG_SIZE : audit_size; f->level_debug = log_level; f->level_audit = audit_level; } #ifdef _WIN32 ReleaseMutex(am_log_lck.lock); #else pthread_mutex_unlock(&log->lock); #endif if (exist == AM_DONE) { #define AM_LOG_HEADER "\r\n\r\n\t######################################################\r\n\t# %-51s#\r\n\t# Version: %-42s#\r\n\t# %-51s#\r\n\t# Build date: %s %-27s#\r\n\t######################################################\r\n" AM_LOG_ALWAYS(instance_id, AM_LOG_HEADER, DESCRIPTION, VERSION, VERSION_VCS, __DATE__, __TIME__); am_agent_init_set_value(instance_id, AM_TRUE, AM_UNKNOWN); } }
void am_log_shutdown(int id) { static const char *thisfunc = "am_log_shutdown():"; int i; int pid = getpid(); struct am_log *log = AM_LOG(); if (log == NULL) { return; } /* notify the logger exit */ for (i = 0; i < AM_MAX_INSTANCES; i++) { struct log_files *f = &log->files[i]; if (f->instance_id > 0 && f->owner == pid) { AM_LOG_ALWAYS(f->instance_id, "%s exiting", thisfunc); } } #ifdef _WIN32 SetEvent(am_log_lck.exit); WaitForSingleObject(am_log_handle->reader_thr, INFINITE); CloseHandle(am_log_lck.exit); CloseHandle(am_log_lck.new_data_cond); CloseHandle(am_log_lck.new_space_cond); WaitForSingleObject(am_log_lck.lock, INFINITE); /* close log file(s) */ for (i = 0; i < AM_MAX_INSTANCES; i++) { struct log_files *f = &log->files[i]; if (f->owner == pid) { if (f->fd_debug != -1) { _close(f->fd_debug); f->fd_debug = -1; } if (f->fd_audit != -1) { _close(f->fd_audit); f->fd_audit = -1; } f->used = AM_FALSE; f->instance_id = 0; f->level_debug = f->level_audit = AM_LOG_LEVEL_NONE; f->max_size_debug = f->max_size_audit = 0; } } ReleaseMutex(am_log_lck.lock); CloseHandle(am_log_lck.lock); UnmapViewOfFile(am_log_handle->area); CloseHandle(am_log_handle->area_file_id); #else pthread_mutex_unlock(&log->exit); pthread_join(am_log_handle->reader_thr, NULL); pthread_mutex_destroy(&log->exit); pthread_mutex_destroy(&log->lock); pthread_cond_destroy(&log->new_data_cond); pthread_cond_destroy(&log->new_space_cond); /* close log file(s) */ for (i = 0; i < AM_MAX_INSTANCES; i++) { struct log_files *f = &log->files[i]; if (f->fd_debug != -1) { close(f->fd_debug); f->fd_debug = -1; } if (f->fd_audit != -1) { close(f->fd_audit); f->fd_audit = -1; } f->used = AM_FALSE; f->instance_id = 0; f->level_debug = f->level_audit = AM_LOG_LEVEL_NONE; f->max_size_debug = f->max_size_audit = 0; } if (munmap((char *) am_log_handle->area, am_log_handle->area_size) == -1) { fprintf(stderr, "am_log_shutdown() munmap failed (%d)\n", errno); } close(am_log_handle->area_file_id); if (shm_unlink(am_log_handle->area_file_name) == -1) { fprintf(stderr, "am_log_shutdown() shm_unlink failed (%d)\n", errno); } #endif am_agent_instance_init_release(id, AM_TRUE); free(am_log_handle); am_log_handle = NULL; }
void test_logging(void** state) { am_log_init(AM_SUCCESS); AM_LOG_ALWAYS(getpid(), "Hello world"); }