亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

? ??? ?? C++ C? ??? ?? ??? ???

C? ??? ?? ??? ???

Nov 29, 2024 am 01:00 AM

Creating a Robust Logging System in C

??? ?????? ???? ?? ?? ??? ????? ??? ???? ??? ??? ??? ?????. ??? ? ? ??? C ???????? ?? ??? ???? ????. ??? ??? ?? ???? ???? ?? ????. ???, ??? ?? ??? ? ????? ???? ???? ???? ???? ????.

? ????? ?? ?????? ??? ?? ??? ??? ?? ??? ???? ?? ???? ???? ???? ??? ???????. ????? C? ???? ?? ??? ?? ???? ??? ??? ???? ???? ? ????.

??

  1. ??? ???
  2. ??? ?? ?? ??
  3. ?? ?? ?? ???
  4. ????? ?? ?? ??
  5. ??? ?? ??
  6. ??? ?? ??
  7. ??? ??? ??
  8. ?? ? ?? ??
  9. ??? ?? ?? ??
  10. ?? ?? ??
  11. ?? ? ???
  12. ?? ?? ??
  13. ?? ??? ??
  14. ??? ? ??
  15. ?? ??? ?? ??
  16. ?? ???
  17. ??

??? ???

?? ???? ??? ????? ???? ?? ????? ??? ???. ??? ??? ??? ??? ????? ?? ????? ???? ???. ??? ????? ???? ? ??? ?? ????? ?? ???. ???? ??? ??? ? ????.

??? ?? ? ??? ???? ??? ?? ??? ?? ??? ??? ?????. ???? ?? ??? ???? ??? ?? ???? ??? ??? ???? ??? ? ????. ?? ??? ???? ???? ??? ???? ??? ?? ?????.

??? ??? ???? ?? ??? ?? ??? ??? ? ?? ????? ???????? ?? ??????. ?? ?? ??? ??? ?????? ??? ??? ?? ??? ???? ?? ?? ???? ?? ?? ????. ??? ??? ??? ?? ???? ???? ?? ??? ???? ? ??? ???.

??? ??? ??? ??? ??? ??????. ??? ???? ?? ?? ????? ?? ??, ?? ??? ? ?? ??? ? ?? ??? ??? ? ????. ??? ?? ???? ??? ? ???? ?? ??? ??? ??? ?? ?????.

??? ?? ?? ??

?????? ??? ?? ?? ??? ????? ??? ??? ?? ??? ??????. ??? ??? ??? ?? ??? ????? ??? ?? ?? ??? ??? ?? ?? ?? ?? ??? ? ????.

?? ??(logger.h):

#ifndef LOGGER_H
#define LOGGER_H

#include <stdio.h>
#include <time.h>

// Function prototypes
void log_message(const char* text);

#endif // LOGGER_H

?? ??(logger.c):

#include "logger.h"

void log_message(const char* text) {
    if (!text) {
        fprintf(stderr, "Invalid log message\n");
        return;
    }
    time_t now = time(NULL);
    printf("[%s] %s\n", ctime(&now), text);
}

???(main.c):

#include "logger.h"

int main() {
    log_message("Application started");
    log_message("Performing operation...");
    log_message("Operation completed.");
    return 0;
}

??? ? ??:

??? ????? ????? ????? ?? ??? ?????.

gcc -o app main.c logger.c
./app

?? ??:

[Mon Sep 27 14:00:00 2021
] Application started
[Mon Sep 27 14:00:00 2021
] Performing operation...
[Mon Sep 27 14:00:00 2021
] Operation completed.

? ?? ??? ?? ?? ????? ??? ????. ? ?????? ??? ?? ?? ??? ?? ??? ???. ?? ?? logger.c?? ?? ???? ?? ??? ??? ? ?? logger_test.c?? ?? ???? ??? ? ????. ?? ??? ?? ???? ?? ? ??? ???? ?? ??? ?????.

?? ?? ?????? logger.h? ?? ?? ??? ?? ????? ??, include/? ?? ??? ???? ?? ?? ??? ??? ????? ????? ???. ??? ?? ?? ??? ??? ?? ??? ?? ???? ? ????. ?? ??? ?? ??? ??? ???? ???? ???? ?? API ????? ?? ?? ??? ?????.

?????, ????? ??? ??? ?? ??? ???? ?? ???? ?? ?????. ?? ??, logger.h ? logger.c? ???? ??? ??? ?? ??? ???? ?? ??????. ???? ?? ??? ?? ??? ???? ?? ??? ??? ????? ?????.

?? ?? ?? ??

?? ?? ???? ???? ?? ??? ????? ?? ??? ???? ?? ??? ????. ? ??? ? ?? ?? ?? ?? ??? ??? ? ??? ???? ???? ??? ?? ????? ???.

??(logger.c):

#include "logger.h"
#include <stdio.h>
#include <time.h>
#include <assert.h>

#define BUFFER_SIZE 256
static_assert(BUFFER_SIZE >= 64, "Buffer size is too small");

void log_message(const char* text) {
    char buffer[BUFFER_SIZE];
    time_t now = time(NULL);

    if (!text) {
        fprintf(stderr, "Error: Null message passed to log_message\n");
        return;
    }

    snprintf(buffer, BUFFER_SIZE, "[%s] %s", ctime(&now), text);
    printf("%s", buffer);
}

??: static_assert? ????? C11 ??? ?????. ????? ? ??? ????? ?????.

?? ?? ??? ???? ?? ???? ???? ??? ? ????. ? ?? ??? ?????? ???? ??? ????? ???? ???? ?????. ?? ??, ??? ?? ??? ??? ??? ??? ??? ?? ???? ??? ?????? ???? ? ??? ? ? ????.

?? ??? ?? ??? ??? ????? ?? ?? ??? ?? ??? ???? ???. ? ??? ??? ??? ????? ??? ?? ??? ???? ???? ????? ?????. ?? ??? ??? ??? ?? ?? ??? ???? ?? ??? ??? ? ???? ? ????.

?? ??? ??? ? ?? ??? ?????. ?? ??, NULL ???? ?? ???? ???? ??? ???? "???? ??"? ?? ??? ?? ???? ????? ?? ???? ???? ?????.

????? ?? ?? ??

??????? ????? ?? ?? ??? ?? ?? ? ????. ??? ??? ?? ?? ??? ??? ??? ?? ?? ??? ???? ??? ? ????. ??? ???? ??? ??? ?????.

?? ????? ???? ??? ???? ????? ?????. ?? ?? ???? ??? ?? ?? ???? ??? ?? ????? ??? ?? ????. ???? ?? ??? ???? ?? ??? ??? ?????.

?? ??? ?? ????? ???? ???? ?????. ??? ???? ?? ? ??? ??? ????? ??? ?????. ??? ?? ?? ?? ??? ?????. ? ?? ??? ?? ??? ???? ???? ?? ??? ?????. ??? ??? ??? ????.

?? ??(logger.h):

#ifndef LOGGER_H
#define LOGGER_H

#include <stdio.h>
#include <time.h>

// Function prototypes
void log_message(const char* text);

#endif // LOGGER_H

?? ??(logger.c):

#include "logger.h"

void log_message(const char* text) {
    if (!text) {
        fprintf(stderr, "Invalid log message\n");
        return;
    }
    time_t now = time(NULL);
    printf("[%s] %s\n", ctime(&now), text);
}

? ??? ???? ??? ??? ??? ???? ??? ??? ?? ??? ???? ?????.

??? ?? ??

??? ??? ??? ???? ??? ??? ???? ??? ???? ??? ? ??????. ?? ?? ?? ?? ?????? ??? ????? ??? ??? ??? ??? ? ????. ??????? ?? ????? ????? ?? ?? ? ??? ???? ?? ??? ????.

?? ???? ? ?? ??? ?? ??? ???? ????. ???? ???? DEBUG, INFO, WARNING ? ERROR? ?????. ?? ??? ?? ??? ?? ?? ?? ??? ??? ? ??? ?? ??? ?? ???? ????? ???? ???? ??? ?????. ? ?? ??? ???? ?? ?? ???? ????? ??? ???? ???? ??? ? ????.

?? ?? ???? ??? ?? ??? ???? ?? ?? ???? ??? ? ????. ?? ?? ??????? ?? ???? ??? ?? ?? ?? ? ???? ???? ??? ? ????.

?? ??(logger.h):

#include "logger.h"

int main() {
    log_message("Application started");
    log_message("Performing operation...");
    log_message("Operation completed.");
    return 0;
}

?? ??(logger.c):

gcc -o app main.c logger.c
./app

? ??? ?? ??? ??? ??? ?? ??? ? ????. ?? ?? ?? ?? ?? ?? ?? ??? DEBUG? ???? ??????? ??? ??? ? ????.

??? ?? ??

??? ??? ??? ?? ?? ???? ?? ?? ??? ??? ? ?????. ??? ?? ???? ??? ???? ???? ??? ??? ??? ???? ??? ??? ?? ??? ??? ??? ? ????.

??? ?? ?? ?? ??? ? ?? ???? ??? ??? ???? ?????. ?? ?? ???? ????? ???? ??? ???? ??? ? ????.

??(logger.c):

#ifndef LOGGER_H
#define LOGGER_H

#include <stdio.h>
#include <time.h>

// Function prototypes
void log_message(const char* text);

#endif // LOGGER_H

???(main.c):

#include "logger.h"

void log_message(const char* text) {
    if (!text) {
        fprintf(stderr, "Invalid log message\n");
        return;
    }
    time_t now = time(NULL);
    printf("[%s] %s\n", ctime(&now), text);
}

??? ? ??:

#include "logger.h"

int main() {
    log_message("Application started");
    log_message("Performing operation...");
    log_message("Operation completed.");
    return 0;
}

??? ?? ?? ???? application.log? ?????. init_logging ? close_logging ??? ???? ??????? ??? ??? ?? ??? ??? ? ?? ?? ? ??? ??? ??? ? ????.

??? ??? ??

????? ???????? ?? ??? ?? ??? ???? ?? ???? ??????? ???? ??? ?????? ???? ???.

??? ???? ???? ? ?? ??? ???? ?? ??? ????? ???? ????.

??(logger.c):

gcc -o app main.c logger.c
./app

????? ????? ??(main.c):

[Mon Sep 27 14:00:00 2021
] Application started
[Mon Sep 27 14:00:00 2021
] Performing operation...
[Mon Sep 27 14:00:00 2021
] Operation completed.

??? ? ??:

#include "logger.h"
#include <stdio.h>
#include <time.h>
#include <assert.h>

#define BUFFER_SIZE 256
static_assert(BUFFER_SIZE >= 64, "Buffer size is too small");

void log_message(const char* text) {
    char buffer[BUFFER_SIZE];
    time_t now = time(NULL);

    if (!text) {
        fprintf(stderr, "Error: Null message passed to log_message\n");
        return;
    }

    snprintf(buffer, BUFFER_SIZE, "[%s] %s", ctime(&now), text);
    printf("%s", buffer);
}

??? ?? ?? ?? ???? ??? ?? ???? ?? ?? ???? ???? ?????.

?? ? ?? ??

?? ??? ???? ??? ? ??? ???? ???? ?????. ?? ??, ???? ??, ??? ?? ??? ?? ???? ????? ??? ??? ?? ??? ? ????.

?? ??(config.cfg):

#ifndef LOGGER_H
#define LOGGER_H

#include <stdbool.h>

void enable_module(const char* module);
void disable_module(const char* module);
void log_message(const char* module, const char* text);

#endif // LOGGER_H

??(logger.c):

#include "logger.h"
#include <stdio.h>
#include <string.h>

#define MAX_MODULES 10
#define MODULE_NAME_LENGTH 20

static char enabled_modules[MAX_MODULES][MODULE_NAME_LENGTH];

void enable_module(const char* module) {
    for (int i = 0; i < MAX_MODULES; i++) {
        if (enabled_modules[i][0] == '<pre class="brush:php;toolbar:false">#ifndef LOGGER_H
#define LOGGER_H

typedef enum { DEBUG, INFO, WARNING, ERROR } LogLevel;

void set_log_level(LogLevel level);
void log_message(LogLevel level, const char* module, const char* text);

#endif // LOGGER_H
') { strncpy(enabled_modules[i], module, MODULE_NAME_LENGTH - 1); enabled_modules[i][MODULE_NAME_LENGTH - 1] = '
#include "logger.h"
#include <stdio.h>
#include <time.h>
#include <string.h>

static LogLevel current_log_level = INFO;

void set_log_level(LogLevel level) {
    current_log_level = level;
}

void log_message(LogLevel level, const char* module, const char* text) {
    if (level < current_log_level) {
        return;
    }
    const char* level_strings[] = { "DEBUG", "INFO", "WARNING", "ERROR" };
    time_t now = time(NULL);
    printf("[%s][%s][%s] %s\n", ctime(&now), level_strings[level], module, text);
}
'; break; } } } void disable_module(const char* module) { for (int i = 0; i < MAX_MODULES; i++) { if (strcmp(enabled_modules[i], module) == 0) { enabled_modules[i][0] = '
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>

static FILE* log_file = NULL;

void init_logging(const char* filename) {
    if (filename) {
        log_file = fopen(filename, "a");
        if (!log_file) {
            fprintf(stderr, "Failed to open log file: %s\n", filename);
            exit(EXIT_FAILURE);
        }
    } else {
        log_file = stdout; // Default to standard output
    }
}

void close_logging() {
    if (log_file && log_file != stdout) {
        fclose(log_file);
        log_file = NULL;
    }
}

void log_message(const char* text) {
    if (!log_file) {
        fprintf(stderr, "Logging not initialized.\n");
        return;
    }
    time_t now = time(NULL);
    fprintf(log_file, "[%s] %s\n", ctime(&now), text);
    fflush(log_file); // Ensure the message is written immediately
}
'; break; } } } static int is_module_enabled(const char* module) { for (int i = 0; i < MAX_MODULES; i++) { if (strcmp(enabled_modules[i], module) == 0) { return 1; } } return 0; } void log_message(const char* module, const char* text) { if (!is_module_enabled(module)) { return; } time_t now = time(NULL); printf("[%s][%s] %s\n", ctime(&now), module, text); }

???(main.c):

#include "logger.h"

int main() {
    init_logging("application.log");

    log_message("Application started");
    log_message("Performing operation...");
    log_message("Operation completed.");

    close_logging();
    return 0;
}

??? ? ??:

gcc -o app main.c logger.c
./app

?? ??? ???? ??????? ?? ????? ??? ?? ??? ??? ? ??? ?? ?? ???? ???? ?????.

??? ?? ?? ??

?? ??? ??? ?????? ?? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ? ????.

??(logger.c):

#include "logger.h"
#include <pthread.h>

static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;

void log_message(const char* text) {
    pthread_mutex_lock(&log_mutex);
    // Existing logging code
    if (!log_file) {
        fprintf(stderr, "Logging not initialized.\n");
        pthread_mutex_unlock(&log_mutex);
        return;
    }
    time_t now = time(NULL);
    fprintf(log_file, "[%s] %s\n", ctime(&now), text);
    fflush(log_file);
    pthread_mutex_unlock(&log_mutex);
}

?? ??:

#include "logger.h"
#include <pthread.h>

void* thread_function(void* arg) {
    char* thread_name = (char*)arg;
    for (int i = 0; i < 5; i++) {
        char message[50];
        sprintf(message, "%s: Operation %d", thread_name, i + 1);
        log_message(message);
    }
    return NULL;
}

int main() {
    init_logging("application.log");

    pthread_t thread1, thread2;

    pthread_create(&thread1, NULL, thread_function, "Thread1");
    pthread_create(&thread2, NULL, thread_function, "Thread2");

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    close_logging();
    return 0;
}

???? ??? ?? JSON ???? ?? ??? ?????.

gcc -pthread -o app main.c logger.c
./app

? ??? ?? ?? ??? ?? ???? ? ?????.

?? ?? ??

?? ??? ???? ?? ?? ??, ??? ?? ?? ?? ??? ??? ? ????. ??? ??? ???? ???? ????? ???? ???? ?? ?????.

??(logger.c):

#ifndef LOGGER_H
#define LOGGER_H

#include <stdio.h>
#include <time.h>

// Function prototypes
void log_message(const char* text);

#endif // LOGGER_H

???? ?? ??? ??? ???? ?? ?? ?? ???? ?????? ??? ???? ?? ??? ??? ?? ??? ??? ? ? ????.

??? ???

??? ?????? ??? ??? ?? ? ???, ?? ??? ?????? ????? ???? ?? ?? ?????. ?? ????? ?? ??? ?? ?? ??? ?????? ???? ?? ??? ?????.

??? ?? ??(logger.c):

#include "logger.h"

void log_message(const char* text) {
    if (!text) {
        fprintf(stderr, "Invalid log message\n");
        return;
    }
    time_t now = time(NULL);
    printf("[%s] %s\n", ctime(&now), text);
}

???(main.c):

#include "logger.h"

int main() {
    log_message("Application started");
    log_message("Performing operation...");
    log_message("Operation completed.");
    return 0;
}

??? ??? ???? ?? ?????? ???? ??? ???? ??? ???? ???? ??? ?????.

?? ?? ??

??? ????? ?? ???? ?? ??? ??? ??? ??? ? ????. ??? ??? ???? ?? ?? ?????? ?? ??? ???? ?? ?????.

??(logger.c):

gcc -o app main.c logger.c
./app

?? ?? ??:

[Mon Sep 27 14:00:00 2021
] Application started
[Mon Sep 27 14:00:00 2021
] Performing operation...
[Mon Sep 27 14:00:00 2021
] Operation completed.

????:

  • ?? ??: ??? ???? ?? ???? ???? ???? ?????.
  • ??? ??: ?? ??? ?? ??? ??? ???? ???? ?????.
  • ???: ?? ??? ??? ??? ??? ?? ???? ?????.
  • ?? ??: ??? ??? ???? ?? ???? ??? ???? ?? ?? ??? ?????.

??? ??? ??? ??????? ??? ???? ??? ?? ??? ??? ? ????.

?? ??? ??

?? ??????? ? ?? ?? ?? ? ??? ?? ?? ?? ?? ? ???? ???? ??? ????.

Syslog ??(logger.c):

#include "logger.h"
#include <stdio.h>
#include <time.h>
#include <assert.h>

#define BUFFER_SIZE 256
static_assert(BUFFER_SIZE >= 64, "Buffer size is too small");

void log_message(const char* text) {
    char buffer[BUFFER_SIZE];
    time_t now = time(NULL);

    if (!text) {
        fprintf(stderr, "Error: Null message passed to log_message\n");
        return;
    }

    snprintf(buffer, BUFFER_SIZE, "[%s] %s", ctime(&now), text);
    printf("%s", buffer);
}

???(main.c):

#ifndef LOGGER_H
#define LOGGER_H

#include <stdbool.h>

void enable_module(const char* module);
void disable_module(const char* module);
void log_message(const char* module, const char* text);

#endif // LOGGER_H

?? ?? ???:

Graylog? Elasticsearch? ?? ?? ???? ??? ???? ???? ???? ?? ?????? ??? ? ????.

?? ?? ?(logger.c):

#include "logger.h"
#include <stdio.h>
#include <string.h>

#define MAX_MODULES 10
#define MODULE_NAME_LENGTH 20

static char enabled_modules[MAX_MODULES][MODULE_NAME_LENGTH];

void enable_module(const char* module) {
    for (int i = 0; i < MAX_MODULES; i++) {
        if (enabled_modules[i][0] == '<pre class="brush:php;toolbar:false">#ifndef LOGGER_H
#define LOGGER_H

typedef enum { DEBUG, INFO, WARNING, ERROR } LogLevel;

void set_log_level(LogLevel level);
void log_message(LogLevel level, const char* module, const char* text);

#endif // LOGGER_H
') { strncpy(enabled_modules[i], module, MODULE_NAME_LENGTH - 1); enabled_modules[i][MODULE_NAME_LENGTH - 1] = '
#include "logger.h"
#include <stdio.h>
#include <time.h>
#include <string.h>

static LogLevel current_log_level = INFO;

void set_log_level(LogLevel level) {
    current_log_level = level;
}

void log_message(LogLevel level, const char* module, const char* text) {
    if (level < current_log_level) {
        return;
    }
    const char* level_strings[] = { "DEBUG", "INFO", "WARNING", "ERROR" };
    time_t now = time(NULL);
    printf("[%s][%s][%s] %s\n", ctime(&now), level_strings[level], module, text);
}
'; break; } } } void disable_module(const char* module) { for (int i = 0; i < MAX_MODULES; i++) { if (strcmp(enabled_modules[i], module) == 0) { enabled_modules[i][0] = '
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>

static FILE* log_file = NULL;

void init_logging(const char* filename) {
    if (filename) {
        log_file = fopen(filename, "a");
        if (!log_file) {
            fprintf(stderr, "Failed to open log file: %s\n", filename);
            exit(EXIT_FAILURE);
        }
    } else {
        log_file = stdout; // Default to standard output
    }
}

void close_logging() {
    if (log_file && log_file != stdout) {
        fclose(log_file);
        log_file = NULL;
    }
}

void log_message(const char* text) {
    if (!log_file) {
        fprintf(stderr, "Logging not initialized.\n");
        return;
    }
    time_t now = time(NULL);
    fprintf(log_file, "[%s] %s\n", ctime(&now), text);
    fflush(log_file); // Ensure the message is written immediately
}
'; break; } } } static int is_module_enabled(const char* module) { for (int i = 0; i < MAX_MODULES; i++) { if (strcmp(enabled_modules[i], module) == 0) { return 1; } } return 0; } void log_message(const char* module, const char* text) { if (!is_module_enabled(module)) { return; } time_t now = time(NULL); printf("[%s][%s] %s\n", ctime(&now), module, text); }

???(main.c):

#include "logger.h"

int main() {
    init_logging("application.log");

    log_message("Application started");
    log_message("Performing operation...");
    log_message("Operation completed.");

    close_logging();
    return 0;
}

?? ???? ??? ?? ?? ??? ?? ??, ??? ????, ??? ?? ?? ??? ??? ? ????.

??? ? ??

??? ???? ?? ??? ???? ?? ???? ???? ????? ?????.

?? ??? ?(test_logger.c):

gcc -o app main.c logger.c
./app

??? ??? ? ??:

#include "logger.h"
#include <pthread.h>

static pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;

void log_message(const char* text) {
    pthread_mutex_lock(&log_mutex);
    // Existing logging code
    if (!log_file) {
        fprintf(stderr, "Logging not initialized.\n");
        pthread_mutex_unlock(&log_mutex);
        return;
    }
    time_t now = time(NULL);
    fprintf(log_file, "[%s] %s\n", ctime(&now), text);
    fflush(log_file);
    pthread_mutex_unlock(&log_mutex);
}

??? ??:

  • ?? ???: ?? ??? ?????.
  • ???? ???: ??? ?? ??? ????????.
  • ????? ???: ?? ????? ??? ?????.
  • ?? ??: ??? ?? ?, ???? ?? ?? ??? ????????.

?? ???? ???? ????? ??? ???? ??? ??? ??? ?? ?? ???? ??? ? ????.

??? ??? ?? ??

?? ??????? ?? ??? ???? ?????. ?? ??? Unix ?? ????? ? ????? ?? ?? API? ??? ?? Windows??? ???? ?? ? ????. ? ??? ????? ??? ??? ?? ????? ?????.

??(logger.c):

#ifndef LOGGER_H
#define LOGGER_H

#include <stdio.h>
#include <time.h>

// Function prototypes
void log_message(const char* text);

#endif // LOGGER_H

???(logger.c):

#include "logger.h"

void log_message(const char* text) {
    if (!text) {
        fprintf(stderr, "Invalid log message\n");
        return;
    }
    time_t now = time(NULL);
    printf("[%s] %s\n", ctime(&now), text);
}

???? ????? ???? ?? ?? ??? ???? ???? ?????.

?? ?? ???

?? ???? ???? ?? ?? ???? ??? ???? ?? ? ???, ??? ??? ??, ?? ? ?? ?? ???? ??? ??? ??? ??? ?????. ??? ??? ???? ?? ??? ???? ???? ?? ???? ???? ?? ?? ???? ?? ? ????.

?? ???? ??? ??? ??? ???? ? ??? ?? ??? ???? ??? ?? ??? ?????. ???? ???? ??? ?????, ?? ??? ?? ??? ??? ????, ?? ??? ????, ???? ???? ??? ? ????. ??? ???? ????, ?? ??? ????, ??? ?? ??? ????, ?? ?? ??? ?????.

?? ??? ???, ?? ????? ? ??? ??? ?? ??? ?????? ???? ??? ??? ?????? ?? ????? ?? ? ????. ??? ?????? ???? ??? ???????? ???? ??? ??? ?? ?????.

? ??? ?? ???? ???? ? ??? ??? ??? ?? ??, ?????? ??? ?? ? ?? ???, ????? ??? ????? ??? ????. ??? ??? ?? ?? ?? ??? ????? ?? ?? ??? ??? ? ????.

??: ?? ??? ??

? ?? ????? ??? ??? ?? ???? ???? ?? ?? ??? ??? ??? ? ?? ??? ?? ????. ??? ?? ??? ??, ?? ?? ??, ??? ?? ???, ??? ? ?? ??? ??? ? ????. ? ???? ?? ???, ???? ? ?? ?? ??, ?? ??? ?? ?? ?? ??? ???? ????.

1. ?? ??? ? ??

???? ?? ??? ?? ??? ???? ?? ???? ??????. C ??????? ?? ???? snake_case? ???? ?? ? ?? ??? ????????.

????? ??(logger.h):

#ifndef LOGGER_H
#define LOGGER_H

#include <stdio.h>
#include <time.h>

// Function prototypes
void log_message(const char* text);

#endif // LOGGER_H

????? ??(logger.c):

#include "logger.h"

void log_message(const char* text) {
    if (!text) {
        fprintf(stderr, "Invalid log message\n");
        return;
    }
    time_t now = time(NULL);
    printf("[%s] %s\n", ctime(&now), text);
}

????? ???(main.c):

#include "logger.h"

int main() {
    log_message("Application started");
    log_message("Performing operation...");
    log_message("Operation completed.");
    return 0;
}

??? ? ??:

gcc -o app main.c logger.c
./app

?? ????:

  • GNU ?? ??: ?? ??
  • Linux ?? ?? ???

2. ??? ?? ??

??? ?? ?? ??? ?? ??????? ??? ?? ??? ???? ??? ? ????.

??? ?? ??(logger.c):

[Mon Sep 27 14:00:00 2021
] Application started
[Mon Sep 27 14:00:00 2021
] Performing operation...
[Mon Sep 27 14:00:00 2021
] Operation completed.

?? ????:

  • C??? ?? ??
  • C??? ???

3. ??? ??? ???

??? ??? ?? ????? ?? ?????? ???? ???? ??? ??????. ?? ??? ?? ??? ???????.

??(logger.c):

#include "logger.h"
#include <stdio.h>
#include <time.h>
#include <assert.h>

#define BUFFER_SIZE 256
static_assert(BUFFER_SIZE >= 64, "Buffer size is too small");

void log_message(const char* text) {
    char buffer[BUFFER_SIZE];
    time_t now = time(NULL);

    if (!text) {
        fprintf(stderr, "Error: Null message passed to log_message\n");
        return;
    }

    snprintf(buffer, BUFFER_SIZE, "[%s] %s", ctime(&now), text);
    printf("%s", buffer);
}

???(main.c):

#ifndef LOGGER_H
#define LOGGER_H

#include <stdbool.h>

void enable_module(const char* module);
void disable_module(const char* module);
void log_message(const char* module, const char* text);

#endif // LOGGER_H

??? ? ??:

#include "logger.h"
#include <stdio.h>
#include <string.h>

#define MAX_MODULES 10
#define MODULE_NAME_LENGTH 20

static char enabled_modules[MAX_MODULES][MODULE_NAME_LENGTH];

void enable_module(const char* module) {
    for (int i = 0; i < MAX_MODULES; i++) {
        if (enabled_modules[i][0] == '<pre class="brush:php;toolbar:false">#ifndef LOGGER_H
#define LOGGER_H

typedef enum { DEBUG, INFO, WARNING, ERROR } LogLevel;

void set_log_level(LogLevel level);
void log_message(LogLevel level, const char* module, const char* text);

#endif // LOGGER_H
') { strncpy(enabled_modules[i], module, MODULE_NAME_LENGTH - 1); enabled_modules[i][MODULE_NAME_LENGTH - 1] = '
#include "logger.h"
#include <stdio.h>
#include <time.h>
#include <string.h>

static LogLevel current_log_level = INFO;

void set_log_level(LogLevel level) {
    current_log_level = level;
}

void log_message(LogLevel level, const char* module, const char* text) {
    if (level < current_log_level) {
        return;
    }
    const char* level_strings[] = { "DEBUG", "INFO", "WARNING", "ERROR" };
    time_t now = time(NULL);
    printf("[%s][%s][%s] %s\n", ctime(&now), level_strings[level], module, text);
}
'; break; } } } void disable_module(const char* module) { for (int i = 0; i < MAX_MODULES; i++) { if (strcmp(enabled_modules[i], module) == 0) { enabled_modules[i][0] = '
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>

static FILE* log_file = NULL;

void init_logging(const char* filename) {
    if (filename) {
        log_file = fopen(filename, "a");
        if (!log_file) {
            fprintf(stderr, "Failed to open log file: %s\n", filename);
            exit(EXIT_FAILURE);
        }
    } else {
        log_file = stdout; // Default to standard output
    }
}

void close_logging() {
    if (log_file && log_file != stdout) {
        fclose(log_file);
        log_file = NULL;
    }
}

void log_message(const char* text) {
    if (!log_file) {
        fprintf(stderr, "Logging not initialized.\n");
        return;
    }
    time_t now = time(NULL);
    fprintf(log_file, "[%s] %s\n", ctime(&now), text);
    fflush(log_file); // Ensure the message is written immediately
}
'; break; } } } static int is_module_enabled(const char* module) { for (int i = 0; i < MAX_MODULES; i++) { if (strcmp(enabled_modules[i], module) == 0) { return 1; } } return 0; } void log_message(const char* module, const char* text) { if (!is_module_enabled(module)) { return; } time_t now = time(NULL); printf("[%s][%s] %s\n", ctime(&now), module, text); }

??:

  • ???-??? ??: ?? ???? ?? ???? ???? ???? ?????. ?? ??? ???? ???? ???? ???? ?? ??? ?????.
  • ??? ???: ??? ? ?? ??? ?? ???? ?? ?????? ??? ???? ?????.
  • ???? ??:logging_active ???? ?? ??? ??? ?? ? ??? ???? ????? ??? ????.

?? ????:

  • ???-??? ??
  • POSIX ??? ?????

4. ??? ? ?? ??

???? ??? ???? ?? ???? ???? ????? ???? ? ?????.

Unity ??? ????? ??:

Unity? C? ?? ??? ????????.

??:

  1. ?? ????? Unity? ???????. GitHub? Unity
  2. ??? ??? unity.h? ?????.

??? ??(test_logger.c):

#include "logger.h"

int main() {
    init_logging("application.log");

    log_message("Application started");
    log_message("Performing operation...");
    log_message("Operation completed.");

    close_logging();
    return 0;
}

??? ??? ? ??:

gcc -o app main.c logger.c
./app

??:

  • setUp ? TearDown: ??? ?? ? ??? ?? ? ??? ??? ?????.
  • ???: ??? ????? TEST_ASSERT_* ???? ?????.
  • ??? ??: ?????? stdout ? ??? ?? ??? ????.

?? ????:

  • Unity ??? ?????
  • C??? ?? ???

5. ?? ??

?? ???? ??? ???? ?? ?? ??? ???? ??? ? ??????.

TLS? ?? ?? ??:

????? ?? ??? ???? ????? TLS ???? ?????.

OpenSSL(logger.c)? ??? ??:

#ifndef LOGGER_H
#define LOGGER_H

#include <stdio.h>
#include <time.h>

// Function prototypes
void log_message(const char* text);

#endif // LOGGER_H

?? ????:

  • OpenSSL ??
  • OpenSSL? ?? ??? ?????

??? ?? ?? ??:

?? ???? ??? ? GDPR? ?? ??? ????? ?????.

????:

  • ???: ???? ?? ???? ????? ????.
  • ??? ??: ?? ??? ?? ???? ?????.
  • ??? ?? ??: ?? ?? ??? ?????.

?? ????:

  • EU GDPR ??
  • HIPAA ?? ??

6. ?? ?? ????? ??

??? ? ??? ?? ?????? ???? ??? ???? ?? ??? ??? ? ????.

zlog ??:

zlog? ????? ?????? ???? ??? ?? ??? C? ?? ????????.

??:

  • ??? ?? ??
  • ?? ?? ???? ? ??? ?????.
  • ??? ?? ??.

?? ?:

  1. ??:
#include "logger.h"

void log_message(const char* text) {
    if (!text) {
        fprintf(stderr, "Invalid log message\n");
        return;
    }
    time_t now = time(NULL);
    printf("[%s] %s\n", ctime(&now), text);
}
  1. ?? ??(zlog.conf):
#include "logger.h"

int main() {
    log_message("Application started");
    log_message("Performing operation...");
    log_message("Operation completed.");
    return 0;
}
  1. ??(main.c):
gcc -o app main.c logger.c
./app
  1. ??? ? ??:
[Mon Sep 27 14:00:00 2021
] Application started
[Mon Sep 27 14:00:00 2021
] Performing operation...
[Mon Sep 27 14:00:00 2021
] Operation completed.

?? ????:

  • zlog ?? ????
  • log4c ????

?? ??? ??:

  • ????? ??? ??:

    • ?? ??? ?????.
    • ?? ??? ?????.
    • ? ????? ?????????.
  • ??:

    • ???? ??? ??? ? ????.
    • ?? ???? ?????.
    • ?? ??? ?? ???? ?????.

7. ?? ??

???? ?? ??? ???? ?? ??? ???????.

?? ??:

??? ?? ???? ???? ?? ????? ??? ??? ?????. ?? ???, ?? ??, ???, ???, ??? ??? ?? ??? ?? ?? ??? ?????? ??????? ?? ?? ???? ???? ????? ??? ??? ? ????.

?? ??:

  • ?? ??: ??? ?? ??? ????? ?????.
  • ?? ??: ?? ??, ???, ?? ?? ?? ?? ?? ????? ?????.
  • ?? ?? ??: ?? ? ????? ?? ??? ?? ??? ??? ??? ?? ??? ?????.

?? ??:

  • ??? ??

? ??? C? ??? ?? ??? ???? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? ????? ??
? ?? ??? ????? ???? ??? ??????, ???? ?????? ????. ? ???? ?? ???? ?? ??? ?? ????. ???? ??? ???? ???? ??? ?? admin@php.cn?? ?????.

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

? ??? ?? ?? ?????(SublimeText3)

???

??? ??

??? ????
1597
29
PHP ????
1488
72
NYT ?? ??? ??
130
836
???
std :: Chrono ?? c std :: Chrono ?? c Jul 15, 2025 am 01:30 AM

STD :: Chrono? ?? ?? ??, ?? ?? ??, ?? ?? ? ?? ?? ? ?? ?? ??? ???? C?? ???? ??? ?????. 1. std :: chrono :: system_clock :: now ()? ???? ?? ??? ?? ? ??? ?? ??? ???? ?? ? ? ??? ??? ??? ???? ?? ?? ? ????. 2. std :: Chrono :: steady_clock? ???? ?? ??? ???? ?? ??? ???? duration_cast? ?? ?? ?, ? ? ?? ??? ??????. 3. ?? (time_point) ? ?? (??)? ?? ??? ? ? ??? ?? ??? ? ?? epoch (epoch)???? ???????.

C? ??? ???? ?????? C? ??? ???? ?????? Jul 04, 2025 am 01:09 AM

???? ?????? ??? ?? ???? ?? ? ??? ????? ???? ????? ??????. 1. ??? ?? ???? ????, ?? ??? ?? ?? ??? ????? (??? ?? C? std :: atomic? ?????). 2. ? ???? ??? ?? ? ?? ?? ???? ?? ???. 3. ??? ?? ??? ???? ???? ??? ????? ?? ? ??? ????? ??????. 4. ?????,? ?? ??? ?? ????? ?? ?? ??? ??? ?? ?? ?? ???? ?????. 5. ??? ?? ?? ??? ?? ? ? ??? ??? ??? ??? ??? ????.

C?? ?? ??? ?? ??? C?? ?? ??? ?? ??? Jul 07, 2025 am 01:41 AM

C : 1?? ?? ????? ?? ??? ?? ??? ????. Linux ????? Backtrace ? Backtrace_symbols ??? ??????. ?? ?? ? ?? ?? ??? ???? ??? ? ? -rdynamic ?? ??? ???????. 2. Windows ????? CaptUreStackBackTrace ??? ???? DBGHELP.LIB? ???? PDB ??? ???? ?? ??? ?? ???????. 3. GoogleBreakPad ?? Boost.StackTrace? ?? ?? ?????? ???? ?? ??? ? ?? ?? ??? ??????. 4. ?? ???? ?? ??? ???? ?? ???? ?? ??? ???? ?????.

C?? Python? ???? ??? C?? Python? ???? ??? Jul 08, 2025 am 12:40 AM

C?? Python Code? ????? ?? ???? ??? ? ?? ???, ?? ?? ?? ??? ???? ?? ??? ?? ? ? ????. 1. Py_Initialize ()? ?????? ????? py_finalize ()? ????. 2. pyrun_simplefile? ???? ??? ?? ?? pyrun_simplefile? ?????. 3. pyimport_importmodule? ?? ?? ?? ??, pyobject_getattrstring? ?? ??? ???? py_buildvalue? ?? ??? ???? ??? ???? ???? ??

C? ??? ??? ??? ?????? C? ??? ??? ??? ?????? Jul 05, 2025 am 01:44 AM

functionhiding alkes ressaMenaMeAsabaseClassFunctions? henaderivedClassDefinesAftunction, theBaseInAccessibleThroughTheDerivedClass.thishAppenswhentheBaseFunctionis notvirtualorsignaturesdon n'tmatchforevered, and nousingdeclarationis

C? POD (?? ?? ???) ??? ?????? C? POD (?? ?? ???) ??? ?????? Jul 12, 2025 am 02:15 AM

C??, POD (PANDALDATA) ??? ??? ??? ?? ??? ???? C ?? ??? ??? ?????. ??? ? ?? ??? ????????. ??? ??? ?? ???? ???, ?? memcpy? ?? ?? ? ? ????. ?? ????? ?? ??? ??? ??? ? ????. ?? ?? ???? ??? ?????. ?? ? ?? ??? ??, ??? ?? ??? ?? ???, ?? ?? ?? ?? ??? ?? ? ?? ? ?? ?? ??? ?????. ?? ?? structpoint {intx; inty;}? pod???. ??? ???? ???? I/O, C ?? ???, ?? ??? ?? ?????. std :: is_pod? ?? ??? POD?? ??? ? ??? C 11 ??? std :: is_trivia? ???? ?? ????.

C? ? ??? ? ?????? C? ? ??? ? ?????? Jul 09, 2025 am 02:38 AM

anullpointerinc isaspecialValueindicating thatapointerspointtoanyvalidmorylocation, anditusiusedToSafelyManageNageanDcheckPointersbeforedEereferencing.1.Beforec 11,0ornull? WASSED, BUTNULLPTRISFREFERREDFORITYONDTYPESAFETY.SUNULLPOINTETYTETETENULUNULPENTETETETENGE

C?? ??? ?? ??? ???? ??? ?????? C?? ??? ?? ??? ???? ??? ?????? Jul 12, 2025 am 01:34 AM

C?? ??? ?? ??? ???? ? ?? ?? ??? ???? : ?? ??? ??, std :: ?? ? ?? ??? ? ??? ???. 1. ?? ???? ?? ???? ???? ??? ???? ?? C ?????? ????? ???? ?? ????. 2. STD :: LAMBDA ???? ?? ? ??? ?? C?? ???? ???? ??? ?? ??? ??? ???? ??-?????. 3. ??? ?? ??? ?? ???? ????? ?? ?? ?? ??? ????? ??? ??? ?? ??? ???? ? ????. ????? ???? ??? std :: ?? ?? ???? ?? ??????? ?? ???? ?? ?? ? ? ????.

See all articles