void ensure_file_size(rollingfile_t* rf) { string_buffer_t bak1; string_buffer_t bak2; size_t n; if(rf->current_size < rf->max_size) { if(0 == rf->out) { rf->out = fopen(rf->nm, "a+"); if(0 == rf->out) printf("打开日志文件 '%s' 失败 - %s", rf->nm, strerror(errno)); } return; } if(0 != rf->out) { fclose(rf->out); rf->out = 0; } string_buffer_init(&bak1); string_buffer_init(&bak2); string_buffer_assign(&bak2, rf->nm); string_buffer_append_sprintf(&bak2,".%u", rf->remain_count); remove(string_data(&bak2)); for(n = rf->remain_count-1; n > 0; --n) { string_buffer_assign(&bak1, rf->nm); string_buffer_append_sprintf(&bak1,".%u", n); if(0 != rename(string_data(&bak1), string_data(&bak2))) debugPrintf(string_data(&bak1), string_data(&bak2)); string_buffer_swap(&bak1, &bak2); } if(0 != rename(rf->nm, string_data(&bak2))) { debugPrintf(rf->nm, string_data(&bak2)); } else { rf->current_size = 0; rf->out = fopen(rf->nm, "a+"); if(0 == rf->out) printf("打开日志文件 '%s' 失败 - %s", rf->nm, strerror(errno)); } string_buffer_destroy(&bak1); string_buffer_destroy(&bak2); }
void JSONCheckingHandler::error(const char *message, ...) { static const char pattern[] = "%what%"; assert(message != NULL); const char *follow_message = message; const char *follow_pattern = &(pattern[0]); char *new_message = NULL; const char *message_to_use = message; while (*follow_message != 0) { if (*follow_message == *follow_pattern) { ++follow_pattern; if (*follow_pattern == 0) { std::string what = get_what(); size_t pre_pattern_char_count = ((follow_message - message_to_use) - (sizeof(pattern) - 2)); size_t what_count = strlen(what.c_str()); char *next = (char *)(malloc( pre_pattern_char_count + what_count + strlen(follow_message))); memcpy(next, message_to_use, pre_pattern_char_count); char *location = next + pre_pattern_char_count; memcpy(location, what.c_str(), what_count); location += what_count; strcpy(location, follow_message + 1); if (new_message != NULL) free(new_message); new_message = next; message_to_use = next; follow_message = location - 1; } } else { follow_pattern = &(pattern[0]); } ++follow_message; } string_buffer buffer; string_buffer_init(&buffer, 10); va_list ap; va_start(ap, message); vbuffer_printf(&buffer, 0, message_to_use, ap); va_end(ap); if (new_message != NULL) free(new_message); throw buffer.array; }
void JSONCheckingHandler::bad_integer(o_integer the_oi, const char *expected, ...) { string_buffer buffer; string_buffer_init(&buffer, 10); va_list ap; va_start(ap, expected); vbuffer_printf(&buffer, 0, expected, ap); va_end(ap); std::string expected_string = buffer.array; free(buffer.array); std::string to_write_string = oi_to_string(the_oi); error("Expected %s for %what%, found the integer %s.", expected_string.c_str(), to_write_string.c_str()); }
static void log_lookup_error(value_t condition, value_t tags, frame_t *frame) { size_t arg_count = get_call_tags_entry_count(tags); string_buffer_t buf; string_buffer_init(&buf); string_buffer_printf(&buf, "%v: {", condition); for (size_t i = 0; i < arg_count; i++) { if (i > 0) string_buffer_printf(&buf, ", "); value_t tag = get_call_tags_tag_at(tags, i); value_t value = frame_get_pending_argument_at(frame, tags, i); string_buffer_printf(&buf, "%v: %v", tag, value); } string_buffer_printf(&buf, "}"); string_t str; string_buffer_flush(&buf, &str); ERROR("%s", str.chars); string_buffer_dispose(&buf); }
void JSONCheckingHandler::bad_number(o_integer mantissa_whole_part, o_integer mantissa_fraction_numerator, size_t mantissa_fraction_digit_count, o_integer exponent, const char *expected, ...) { string_buffer buffer; string_buffer_init(&buffer, 10); va_list ap; va_start(ap, expected); vbuffer_printf(&buffer, 0, expected, ap); va_end(ap); std::string expected_string = buffer.array; free(buffer.array); std::string number_string = oi_to_string(mantissa_whole_part); if (!(oi_equal(mantissa_fraction_numerator, oi_zero))) { number_string += "."; std::string fraction_digits = oi_to_string(mantissa_fraction_numerator); size_t digit_count = strlen(fraction_digits.c_str()); assert(digit_count <= mantissa_fraction_digit_count); while (digit_count < mantissa_fraction_digit_count) { number_string += "0"; ++digit_count; } number_string += fraction_digits; } if (!(oi_equal(exponent, oi_zero))) { number_string += "e"; number_string += oi_to_string(exponent); } error("Expected %s for %what%, found the rational %s.", expected_string.c_str(), number_string.c_str()); }
int mod_clamav_result(struct smtp_server_context *ctx, bfd_t *fr, int status) { if (WEXITSTATUS(status) > 1) { JS_Log(JS_LOG_ERR, "clamdscan failed with error\n"); return 0; } if (!WEXITSTATUS(status)) { JS_Log(JS_LOG_INFO, "message passed\n"); return 0; } ctx->code = 550; do { struct string_buffer sb; char c; int i; string_buffer_init(&sb); /* first line of output is "stream: " followed * by the virus name followed by " FOUND"; first * skip "stream: " */ for (i = 0; i < 8; i++) if (bfd_getc(fr) < 0) break; /* copy virus name */ while ((c = bfd_getc(fr)) >= 0 && c != ' ') string_buffer_append_char(&sb, c); if (sb.s == NULL) break; if (asprintf(&ctx->message, "This message appears to be infected with the %s virus", sb.s) == -1) ctx->message = NULL; } while (0); if (ctx->message == NULL) ctx->message = strdup("This message appears to contain viruses"); return 0; }
void JSONCheckingHandler::bad_integer(uint64_t the_integer, const char *expected, ...) { char integer_buffer[21]; integer_buffer[20] = 0; char *position = &(integer_buffer[19]); if (the_integer == 0) { *position = '0'; --position; } else { uint64_t remainder = the_integer; do { assert(position > &(integer_buffer[0])); *position = '0' + (remainder % 10); --position; remainder /= 10; } while (remainder > 0); } string_buffer buffer; string_buffer_init(&buffer, 10); va_list ap; va_start(ap, expected); vbuffer_printf(&buffer, 0, expected, ap); va_end(ap); std::string expected_string = buffer.array; free(buffer.array); error("Expected %s for %what%, found the integer %s.", expected_string.c_str(), position); }
static int do_parse_lists (FILE *h_file, FILE *c_file, const ListDescription *lists) { int k; int result = 1; int had_c_includes = 0; int had_h_includes = 0; int in_list = 0; int equal_to_last = 0; int h_file_line_length = 0; int num_c_file_array_elements = 0; int pending_linefeeds = 0; const char *h_file_enum_name = NULL; const char *c_file_array_name = NULL; char *last_identifier = NULL; char *pending_h_comment = NULL; char *pending_c_comment = NULL; char *pending_eol_comment = NULL; StringBuffer c_file_arrays[NUM_LIST_SORT_ORDERS]; StringBuffer *list_c_file_array = NULL; StringBuffer h_file_enums; string_buffer_init (&h_file_top, 0x2000, 0x1000); string_buffer_init (&h_file_bottom, 0x2000, 0x1000); string_buffer_init (&h_file_enums, 0x2000, 0x1000); string_buffer_init (&c_file_top, 0x2000, 0x1000); string_buffer_init (&c_file_bottom, 0x2000, 0x1000); for (k = 0; k < NUM_LIST_SORT_ORDERS; k++) string_buffer_init (&c_file_arrays[k], 0x2000, 0x1000); while (1) { char *line = read_line (); if (!line) { while (lists->name && lists->multiple_lists_allowed) lists++; if (!condition_stack) { if (!lists->name) { result = 0; if (lists->list_finalizer) { if (lists->list_finalizer (NULL)) result = 1; } } else { fprintf (stderr, "%s: unexpected end of file: list of type `%s' expected\n", short_program_name, lists->name); } } else { fprintf (stderr, "%s: unexpected end of file: condition `%s' unterminated\n", short_program_name, condition_stack->condition->identifier); } break; } if (! *line) continue; if (line[0] == '#') { if (line[1] == '>') { line = line + 2; while (isspace (*line)) line++; utils_free (pending_h_comment); pending_h_comment = utils_duplicate_string (line); utils_free (pending_c_comment); pending_c_comment = utils_duplicate_string (line); } continue; } if (in_list) { if (line[0] != '}') { char first_char = line[0]; const char *identifier = NULL; if (first_char != '=' && first_char != '+') { if (lists->line_parser1) { if (lists->line_parser1 (&line)) break; if (!line) continue; while (*line && isspace (*line)) line++; } } else { if (!h_file_enum_name) { print_error ("`+' and `=' directives are not allowed " "in lists that don't generate enumerations"); break; } do line++; while (isspace (*line)); } if ((!pending_eol_comment || ! *pending_eol_comment) && last_identifier && h_file_enum_name) string_buffer_cat_string (&h_file_enums, ",\n"); if (pending_eol_comment) { if (*pending_eol_comment && h_file_enum_name) { string_buffer_cprintf (&h_file_enums, ",%s/* %s */\n", TABBING (7, h_file_line_length + 1), pending_eol_comment); } utils_free (pending_eol_comment); pending_eol_comment = NULL; } if (pending_h_comment) { if (*pending_h_comment && h_file_enum_name) { if (last_identifier) string_buffer_add_character (&h_file_enums, '\n'); string_buffer_cat_strings (&h_file_enums, " /* ", pending_h_comment, " */\n", NULL); } utils_free (pending_h_comment); pending_h_comment = NULL; } if (h_file_enum_name) { identifier = parse_thing (IDENTIFIER, &line, "identifier"); if (!identifier) break; string_buffer_cat_strings (&h_file_enums, " ", identifier, NULL); h_file_line_length = 2 + strlen (identifier); if (first_char == '=' || equal_to_last) { string_buffer_cat_strings (&h_file_enums, " = ", last_identifier, NULL); h_file_line_length += 3 + strlen (last_identifier); } utils_free (last_identifier); last_identifier = utils_duplicate_string (identifier); } if (first_char != '+') { if (first_char != '=') { if (c_file_array_name && *lists->c_file_array_type) { if (num_c_file_array_elements > 0) { string_buffer_add_character (list_c_file_array, ','); string_buffer_add_characters (list_c_file_array, '\n', 1 + pending_linefeeds); } if (pending_c_comment) { if (*pending_c_comment) { if (num_c_file_array_elements > 0) string_buffer_add_character (list_c_file_array, '\n'); string_buffer_cat_strings (list_c_file_array, " /* ", pending_c_comment, " */\n", NULL); } utils_free (pending_c_comment); pending_c_comment = NULL; } } if (c_file_array_name) num_c_file_array_elements++; pending_linefeeds = 0; if (lists->line_parser2 (list_c_file_array, &line, identifier, &pending_eol_comment, &pending_linefeeds)) break; if (*line) { print_error ("unexpected characters at the end of line"); break; } if (pending_linefeeds < 0) { pending_linefeeds = 0; if (! *line) { while (1) { line = read_line (); if (line && ! *line) pending_linefeeds++; else { reuse_last_line (&line); break; } } } } } equal_to_last = 0; } else { if (equal_to_last) { print_error ("second inserted identifier in a row; " "did you mean `='?"); break; } equal_to_last = 1; } } else { if (!last_identifier && num_c_file_array_elements == 0) { print_error ("empty list `%s'", lists->name); break; } if (pending_eol_comment) { if (*pending_eol_comment && h_file_enum_name) { string_buffer_cprintf (&h_file_enums, "%s/* %s */", TABBING (7, h_file_line_length), pending_eol_comment); } utils_free (pending_eol_comment); pending_eol_comment = NULL; } if (lists->list_finalizer) { if (lists->list_finalizer (list_c_file_array)) break; } if (h_file_enum_name) { if (strcmp (h_file_enum_name, "unnamed") != 0) { string_buffer_cat_strings (&h_file_enums, "\n} ", h_file_enum_name, ";\n", NULL); } else string_buffer_cat_string (&h_file_enums, "\n};\n"); } if (c_file_array_name && *lists->c_file_array_type) string_buffer_cat_string (list_c_file_array, "\n};\n"); if (!lists->multiple_lists_allowed) lists++; in_list = 0; } } else { if (looking_at ("@include", &line) || looking_at ("@c_include", &line)) { if (! *line) { print_error ("filename expected"); break; } if (!had_c_includes) { fputs ("\n\n", c_file); had_c_includes = 1; } fprintf (c_file, "#include %s\n", line); } else if (looking_at ("@h_include", &line)) { if (! *line) { print_error ("filename expected"); break; } if (had_h_includes != 1) { fputs ((had_h_includes == 0 ? "\n\n" : "\n"), h_file); had_h_includes = 1; } fprintf (h_file, "#include %s\n", line); } else if (looking_at ("@define_condition", &line)) { const PredefinedCondition *condition = get_condition (&line); if (!condition) break; if (had_h_includes != 2) { fputs ((had_h_includes == 0 ? "\n\n" : "\n"), h_file); had_h_includes = 2; } fprintf (h_file, "#define %s%s%d\n", condition->identifier, TABBING (4, 8 + strlen (condition->identifier)), condition->value); } else { const char *identifier = parse_thing (IDENTIFIER, &line, "list name"); if (!identifier) break; if (!lists->name) { print_error ("unexpected list beginning"); break; } if (lists->multiple_lists_allowed && strcmp (identifier, lists->name) != 0 && (lists + 1)->name) lists++; if (strcmp (identifier, lists->name) == 0) { if (looking_at ("-", &line)) { if (lists->enumeration_required) { print_error ("enumeration name expected"); break; } h_file_enum_name = NULL; } else { h_file_enum_name = parse_thing (IDENTIFIER, &line, "enumeration name"); if (!h_file_enum_name) break; } if (!lists->c_file_array_type) { if (!looking_at ("-", &line)) { print_error ("unexpected array name"); break; } c_file_array_name = NULL; } else { if (looking_at ("-", &line)) { print_error ("array name expected"); break; } c_file_array_name = parse_thing (IDENTIFIER, &line, "array name"); if (!c_file_array_name) break; } if (*line != '{') { print_error ("list opening brace expected"); break; } if (*(line + 1)) { print_error ("unexpected characters at the end of line"); break; } if (pending_h_comment) { if (*pending_h_comment && h_file_enum_name) { string_buffer_cat_strings (&h_file_enums, "/* ", pending_h_comment, " */\n", NULL); } utils_free (pending_h_comment); pending_h_comment = NULL; } assert (0 <= lists->sort_order && lists->sort_order <= NUM_LIST_SORT_ORDERS); list_c_file_array = &c_file_arrays[lists->sort_order]; if (h_file_enum_name) { if (strcmp (h_file_enum_name, "unnamed") != 0) string_buffer_cat_string (&h_file_enums, "\n\ntypedef enum {\n"); else string_buffer_cat_string (&h_file_enums, "\n\nenum {\n"); } if (c_file_array_name && *lists->c_file_array_type) { string_buffer_cat_strings (list_c_file_array, "\n\n", lists->c_file_array_type, c_file_array_name, "[] = {\n", NULL); } if (lists->list_initializer) { if (lists->list_initializer (list_c_file_array, h_file_enum_name, c_file_array_name)) break; } in_list = 1; equal_to_last = 0; num_c_file_array_elements = 0; pending_linefeeds = 1; utils_free (last_identifier); last_identifier = NULL; } else { print_error ("list name `%s' expected, got `%s'", lists->name, identifier); break; } } } } if (h_file_top.length > 0) fwrite (h_file_top.string, h_file_top.length, 1, h_file); if (h_file_enums.length > 0) fwrite (h_file_enums.string, h_file_enums.length, 1, h_file); if (h_file_bottom.length > 0) fwrite (h_file_bottom.string, h_file_bottom.length, 1, h_file); string_buffer_dispose (&h_file_top); string_buffer_dispose (&h_file_bottom); string_buffer_dispose (&h_file_enums); if (c_file_top.length > 0) fwrite (c_file_top.string, c_file_top.length, 1, c_file); for (k = 0; k < NUM_LIST_SORT_ORDERS; k++) { fwrite (c_file_arrays[k].string, c_file_arrays[k].length, 1, c_file); string_buffer_dispose (&c_file_arrays[k]); } if (c_file_bottom.length > 0) fwrite (c_file_bottom.string, c_file_bottom.length, 1, c_file); string_buffer_dispose (&c_file_top); string_buffer_dispose (&c_file_bottom); utils_free (last_identifier); utils_free (pending_h_comment); utils_free (pending_c_comment); utils_free (pending_eol_comment); string_list_empty (&lines); return result; }