📄 log.c
字号:
}/* mss_log_raw_info(): This function is a helper function to mss_log_info() that prints the info only, without separators and stuff. Also used by mss_exit(). */void mss_log_raw_info(void){ const char format[] = "%-10s %8lu %8lu \n"; mss_disable_threading(); if (mss_num_of_blocks != 0) fprintf(mss_logfile, "INFO: %lu blocks currently allocated.\n", mss_num_of_blocks); else fprintf(mss_logfile, "INFO: NO blocks currently allocated.\n"); if (mss_num_of_blocks != 0) fprintf(mss_logfile, "INFO: %lu bytes of memory currently used.\n", mss_used_mem); fprintf(mss_logfile, "INFO: %lu bytes maximum memory used.\n", mss_max_used_mem); fprintf(mss_logfile, "\n"); fprintf(mss_logfile, "Method Times Called Successful Calls\n"); fprintf(mss_logfile, "---------------------------------------------\n"); fprintf(mss_logfile, format, "new", mss_no.news.calls, mss_no.news.successes); fprintf(mss_logfile, format, "new[]", mss_no.new_arrays.calls, mss_no.new_arrays.successes); fprintf(mss_logfile, format, "delete", mss_no.deletes.calls, mss_no.deletes.successes); fprintf(mss_logfile, format, "delete[]", mss_no.delete_arrays.calls, mss_no.delete_arrays.successes); fprintf(mss_logfile, format, "malloc", mss_no.mallocs.calls, mss_no.mallocs.successes); fprintf(mss_logfile, format, "xmalloc", mss_no.xmallocs.calls, mss_no.xmallocs.successes); fprintf(mss_logfile, format, "realloc", mss_no.reallocs.calls, mss_no.reallocs.successes); fprintf(mss_logfile, format, "xrealloc", mss_no.xreallocs.calls, mss_no.xreallocs.successes); fprintf(mss_logfile, format, "strdup", mss_no.strdups.calls, mss_no.strdups.successes); fprintf(mss_logfile, format, "calloc", mss_no.callocs.calls, mss_no.callocs.successes); fprintf(mss_logfile, format, "free", mss_no.frees.calls, mss_no.frees.successes); fprintf(mss_logfile, format, "cfree", mss_no.cfrees.calls, mss_no.cfrees.successes); fprintf(mss_logfile, format, "xfree", mss_no.xfrees.calls, mss_no.xfrees.successes); mss_enable_threading();}/* mss_log_disable(): Well, figure this out if you can. It will disable all log-output. */void mss_log_disable(const char *filename, const char *function, unsigned long line){ mss_check_if_initialized(); mss_disable_threading(); mss_log(MSS_LOG_MODE_START_SEPARATOR | MSS_LOG_MODE_ALWAYS | MSS_LOG_MODE_END_SEPARATOR, "MSG", "Logging output DISABLED in function %s (line %lu of %s) at %s", function, line, filename, get_time()); mss_options = mss_options & (~MSS_SHOW_LOGS); mss_enable_threading(); mss_deinitialize_if_restarted();}/* mss_enable_log_output(): The very opposite of mss_log_disable. */void mss_log_enable(const char *filename, const char *function, unsigned long line){ mss_check_if_initialized(); mss_disable_threading(); mss_log(MSS_LOG_MODE_START_SEPARATOR | MSS_LOG_MODE_END_SEPARATOR | MSS_LOG_MODE_ALWAYS, "MSG", "Logging output ENABLED in function %s (line %lu of %s) at %s", function, line, filename, get_time()); mss_options = mss_options | MSS_SHOW_LOGS; mss_enable_threading(); mss_deinitialize_if_restarted();}/* mss_log_msg(): This function will output the specified message to the logfile. It will only be called by the user. */void mss_log_msg(const char *msg, const char *filename, const char *function, unsigned long line){ mss_check_if_initialized(); mss_disable_threading(); mss_slog("MSG: FILENAME=\"%s\" FUNCTION=\"%s\" LINE=\"%lu\" MSG=\"%s\"\n", filename, function, line, msg); fprintf(mss_logfile, mss_separator2); fprintf(mss_logfile, "MSG: USER MSG requested in %s (line %lu of %s):\n", function, line, filename); fprintf(mss_logfile, mss_word_wrap("MSG", msg)); fprintf(mss_logfile, "\n"); mss_log(MSS_LOG_MODE_ALWAYS, NULL, mss_separator2); mss_enable_threading(); mss_deinitialize_if_restarted();}/* mss_log_info(): This function prints some information about the current status of the program. */void mss_log_info(const char *filename, const char *function, unsigned long line){ mss_check_if_initialized(); mss_disable_threading(); mss_log(MSS_LOG_MODE_START_SEPARATOR | MSS_LOG_MODE_ALWAYS, "MSG", "Info requested in module %s, line %lu, function %s:\n", filename, line, function); mss_log_raw_info(); mss_log(MSS_LOG_MODE_ALWAYS, NULL, mss_separator); mss_enable_threading(); mss_deinitialize_if_restarted();}/* mss_log_internal_info(): This function will print some internal info about MSS itself, how much memory is being wasted and such. */void mss_log_internal_info(const char *filename, const char *function, unsigned long line){ mss_check_if_initialized(); mss_disable_threading(); mss_log(MSS_LOG_MODE_START_SEPARATOR | MSS_LOG_MODE_ALWAYS, "MSG", "MSS Internal Info requested in %s (line %lu of %s):\n", function, line, filename); fprintf(mss_logfile, "INFO: MSS is internally using %li bytes of memory.\n", (long)(mss_num_of_blocks * sizeof(MssNode))); if (MSS_DO_WATCH_LIMITS) fprintf(mss_logfile, "INFO: WatchLimits guard info is wasting %li bytes of memory.\n", mss_num_of_blocks * MSS_WATCH_LIMITS_SIZE); mss_log(MSS_LOG_MODE_ALWAYS, NULL, mss_separator); mss_enable_threading(); mss_deinitialize_if_restarted();}/* mss_log_list_blocks(): This function will list all blocks allocated, specifying in what function it was allocated, the size, and such. */void mss_log_list_blocks(const char *filename, const char *function, unsigned long line){ MssNode *node; mss_check_if_initialized(); mss_disable_threading(); dcflist_rewind(&mss_list); mss_log(MSS_LOG_MODE_START_SEPARATOR | MSS_LOG_MODE_ALWAYS, "MSG", "Block listing requested in function %s (line %lu of %s):\n", function, line, filename); fprintf(mss_logfile, "LIST: %li blocks currently allocated;\n", mss_num_of_blocks); while ( (node = dcflist_get_item(&mss_list)) != NULL) { sprintf(tmpstr, MSS_PRINTF_SIZE_T " bytes allocated at %p (%s) by function %s (line %lu of %s).\n", (MSS_PTR_SIZE_T)node->size, node->ptr, node->label == NULL ? "no label" : node->label, node->function, node->line, node->filename); fprintf(mss_logfile, mss_word_wrap("LIST", tmpstr)); dcflist_go_forward(&mss_list); } mss_log(MSS_LOG_MODE_ALWAYS, NULL, mss_separator); mss_enable_threading(); mss_deinitialize_if_restarted();}/* mss_log_check_node_warnings: This function will write the appropriate warnings (if any) according to the result code that mss_check_node() returns. Also takes care of special log output. */void mss_log_check_node_warnings(MssNode *node, int result, const char *filename, const char *function, unsigned long line){ (void)filename; (void)line; (void)function; if (result & MSS_CHECK_NODE_PREFIX) { mss_slog("PREFIX: \"Corrupt\" "); mss_warn(MSS_WARN_IF_SHOW | MSS_WARN_NO_EXIT, "Access out of range; PREFIX of block at %p (%s) sized " MSS_PRINTF_SIZE_T " bytes, allocated by %s (line %lu of %s), has been corrupted.\n", node->ptr, node->label == NULL ? "no label" : node->label, (MSS_PTR_SIZE_T)node->size, node->function, node->line, node->filename); } else { mss_slog("PREFIX: \"Ok\" "); } if (result & MSS_CHECK_NODE_SUFFIX) { mss_slog("SUFFIX: \"Corrupt\" "); mss_warn(MSS_WARN_IF_SHOW | MSS_WARN_NO_EXIT, "Access out of range; SUFFIX of block at %p (%s) sized " MSS_PRINTF_SIZE_T " bytes, allocated by %s (line %lu of %s), has been corrupted.\n", node->ptr, node->label == NULL ? "no label" : node->label, (MSS_PTR_SIZE_T)node->size, node->function, node->line, node->filename); } else { mss_slog("SUFFIX: \"Ok\" "); } if (result & MSS_CHECK_NODE_CORRUPT) { mss_slog("CONSTANT=\"Corrupt\" "); mss_warn(MSS_WARN_IF_SHOW | MSS_WARN_NO_EXIT, "Block sized " MSS_PRINTF_SIZE_T " bytes at %p (%s), allocated by %s (line %lu of %s) has been CORRUPTED.\n", (MSS_PTR_SIZE_T)node->size, node->ptr, node->label == NULL ? "no label" : node->label, node->function, node->line, node->filename); } else { mss_slog("CONSTANT=\"%s\" ", (node->type & 1) ? "Ok" : "NA"); }} /* mss_log_query_blocks: ** ** This function traverses the list of allocated blocks and calls ** a user-supplied function parameterized with the block pointer, ** the name of the function that allocated the block and the line ** number that contains the malloc() call, etc. This allows for ** selective inspection of block contents or selective block lists. ** */void mss_log_block_list_filtered(const char *filename, const char *function, unsigned int line, MssCallBackFunc callback_func){ MssNode *node; int stop = 0; if (callback_func == NULL) return; mss_check_if_initialized(); mss_disable_threading(); dcflist_rewind(&mss_list); mss_log(MSS_LOG_MODE_START_SEPARATOR | MSS_LOG_MODE_ALWAYS, "MSG", "Block callback list requested in function %s (line %i of %s):\n", function, line, filename); while (!stop && ((node = dcflist_get_item(&mss_list)) != NULL)) { tmpstr[0] = '\0'; stop = (*callback_func) ( tmpstr, /* Any output must go here */ node->ptr, /* The allocated block */ node->size, /* The block's size */ node->label, /* The block's label */ node->filename, /* The file in which it took place */ node->function, node->line /* The line where it happened */ ); if (tmpstr[0] != '\0') /* Function had something to say... */ fprintf(mss_logfile, mss_word_wrap("LIST", tmpstr)); dcflist_go_forward(&mss_list); } mss_log(MSS_LOG_MODE_ALWAYS, NULL, mss_separator); mss_enable_threading(); mss_deinitialize_if_restarted();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -