// ------------------------------------------------------------------------------------------------- LOG_HANDLE* PsLogOpen(const char* prefix) { LOG_HANDLE *hHandle = (LOG_HANDLE*)malloc(sizeof(LOG_HANDLE)); InitMutex(&hHandle->g_log_mutex); hHandle->g_bytes = 0; hHandle->g_cur_file = NULL; assert(prefix); if (prefix == NULL) { return NULL; } else { const char bad_char[] = "/\\:*?\"<>|"; // 不能用于文件名的字符 int prefix_len = (int)strlen(prefix); int i = 0; memset(hHandle->g_fname_prefix, 0, sizeof(hHandle->g_fname_prefix)); for (; i < prefix_len && i < (int)sizeof(hHandle->g_fname_prefix) - 1; i++) { const char c = prefix[i]; if (strchr(bad_char, c)) hHandle->g_fname_prefix[i] = '#'; else hHandle->g_fname_prefix[i] = c; } } switch_file(hHandle, 0); PsLogAdd_msg(hHandle, "%s.log start", prefix); return hHandle; }
void switch_files(int sig_type) { if (sig_type == SIGUSR1) { write(pipe2[1], file1, strlen(file1)+1); if (switch_file(pipe1[0], &fd1, 1) == SWITCHED) { kill(*pid2_t, SIGUSR2); } } if (sig_type == SIGUSR2) { switch_file(pipe2[0], &fd2, 2); } }
cluster* db::new_cluster() { throw_if_reader(); cluster * new_c = &m_cluster_space[m_header->first_free_cluster]; if ((char*)new_c + sizeof(cluster) >= m_work_file + m_file_size) { switch_file(false); //The file is full. new_c = &m_cluster_space[m_header->first_free_cluster]; return NULL; } m_header->first_free_cluster++; new_c->cur_pos = 0; new_c->next_cluster = (uint)-1; return new_c; }
db::db(int worker_id, uint file_size, bool reader) : m_worker_id(worker_id), m_file_size(file_size), m_cluster_space(NULL), m_file_no(0), m_file_saver_thread(NULL), m_header(NULL), m_work_file(NULL), m_descriptors(NULL), m_reader(reader) { if (!reader) { m_save_file = (char*) malloc(file_size); switch_file(false); } }
db::~db() { if (!m_reader) switch_file(true); //Finalize the current file if (m_file_saver_thread) m_file_saver_thread->join(); if (m_save_file) free(m_save_file); if (m_work_file) free(m_work_file); if (m_file_saver_thread) delete m_file_saver_thread; if (m_descriptors) delete m_descriptors; for (auto desc: m_files_descriptors) { delete desc; } // TODO Auto-generated destructor stub }
// ------------------------------------------------------------------------------------------------- static void switch_file(LOG_HANDLE *hHandle, int isretry) { char filename[512] = {0}; time_t log_time = 0; struct tm *newtime = NULL; if (!hHandle) return; if (hHandle->g_cur_file) { fclose(hHandle->g_cur_file); hHandle->g_cur_file = NULL; } time(&log_time); newtime = localtime(&log_time); if (!isretry) { snprintf(filename, sizeof(filename), ".\\log\\%s_%d-%d-%d.log", hHandle->g_fname_prefix, newtime->tm_year + 1900, newtime->tm_mon + 1, newtime->tm_mday); } else { snprintf(filename, sizeof(filename), ".\\log\\%s_%d-%d-%d_%d-%d-%d.log", hHandle->g_fname_prefix, newtime->tm_year + 1900, newtime->tm_mon + 1, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec); } MkDir("log"); hHandle->g_cur_file = fopen(filename, "ab"); if (!hHandle->g_cur_file) { #ifdef _MSC_VER Sleep(1000); #else usleep(1000 * 1000); #endif switch_file(hHandle, 1); } hHandle->g_bytes = 0; }
// ------------------------------------------------------------------------------------------------- void PsLogAdd(LOG_HANDLE *hHandle, int lv, char *buffer) { if (!hHandle) hHandle = GetMainLog(); #if defined(_DEBUG) && defined(_WIN32) OutputDebugStringA("["); OutputDebugStringA(hHandle->g_fname_prefix); OutputDebugStringA("] "); OutputDebugStringA(buffer); OutputDebugStringA("\r\n"); #endif LockMutex(&hHandle->g_log_mutex); if (hHandle->g_cur_file) { size_t len = strlen(buffer); if (len >= PULOG_LENGTH-1){ buffer[len-1] = '\n'; fwrite(buffer, len-1, 1, hHandle->g_cur_file); }else{ buffer[len] = '\n'; buffer[len+1] = '\0'; fwrite(buffer, len+1, 1, hHandle->g_cur_file); } fflush(hHandle->g_cur_file); hHandle->g_bytes += len; if (hHandle->g_bytes >= MAX_LOG_FILE_SIZE) switch_file(hHandle, 0); gs_default_sh_func(lv, buffer); } UnlockMute(&hHandle->g_log_mutex); return; }
// ------------------------------------------------------------------------------------------------- void MWLogSetFilePrefix(LOG_HANDLE *hHandle, const char *file_prefix) { scpy(hHandle->g_fname_prefix, file_prefix, MAX_PATH); switch_file(hHandle, 0); }