国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

? ??? ?? 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)

???

??? ??

?? ????
1783
16
Cakephp ????
1725
56
??? ????
1577
28
PHP ????
1440
31
???
C ??? : ??? ??? ???? ??? ????? C ??? : ??? ??? ???? ??? ????? Jun 20, 2025 am 12:05 AM

?, ?? ???? C? ??? ??, ?? ??? ? ??????. 1. ?? ???? ??? ??? ?? ?? ??? ?? ?? ??? ?????. 2. ????? ??? ?? ??? ?? ??? ??? ?? ? ??? ?????. 3. ??? ???? ?? ?? ???? ???? ?? ?? ??? ??? ????? ????? ???? ?????.

C? ???? ?????? ???? C? ???? ?????? ???? Jun 20, 2025 am 12:08 AM

C? ? ?? ?? ??? ??? ??? : ??? ?? ??? ? ??? ???. 1. ??? ?? ???? ?? ??? ? ???? ?? ????? ?? ??? ????? ?? ???? ??? ? ????. 2. ??? ???? ?? ?? ? ??? ?? ???? ???? ??? ?? ?? ??? ?????.

C : ???? ?? ?????? C : ???? ?? ?????? Jun 20, 2025 am 12:01 AM

?, C? ???? ?? ?????. 1) ??? ??? ?? ?? ? ??? ???? ?????. 2) ?? ???? ???? ??? ????. 3) ?? ??? ????? ??? ?? ???? ??? ??? ? ??????. ?? ? ??? ?? ???? ???? ??? ?????? ??? ?? ?????.

C ??? : ???? ?? C ??? : ???? ?? Jun 20, 2025 am 12:12 AM

C DestructorsCanleadToSeVeralCommonerrors.toaVoidthem : 1) ?? ?? ?? ?? ?? ?? ???

C? ??? : ????? ??? ? ??? C? ??? : ????? ??? ? ??? Jun 21, 2025 am 12:11 AM

C? ???? ??? ??? ? ??? ?? ????? ????. 1. ??? ???? ?? ??? ?? ????? ???? ??? ??? ???? ?? ? ? ????. 2. ??? ?? ???? ?? ??? ? ???? ?? ????, ??? ???? ?????.

C ???? ?? ?????? ???? C ???? ?? ?????? ???? Jul 01, 2025 am 01:11 AM

?? ???? ??? C?? ??? ??? ???? ??? : ? ????? ? ? ????? C? ??? ? ????? ?? ?? ??? ?? ??? ???? ?????. 1. ?? ??? ???? C? ?? ??? ???? ?? ?? ?? Curly Braces {}? ???? ?? ??? ?? ??? ???????. 2. ?? ??? ? ??? ?? ???? C?? ?? ??? ?? ????? ??? ???? ???? ???? ??? ?????? ???????. RAII ??? ?? ??? ??? ? ????. 3. ?? ? ??? ???? C? ?? ?, ??? ? ???? ?? ??? ??????? ??? ???? ?? ?? ??? ???????. 4. ?? ????? ???? STL? ??? ????? ????? ????? ???? ????? ????? ???????. 5

C?? ??? ??? ???? ?????? C?? ??? ??? ???? ?????? Jun 20, 2025 am 12:21 AM

C polymorphismincludescompile time, ??? ? Templatepolymorphism.1) compile-timepolymorphismusesfunctionandoveroveroverforlogy

C ??? : ?? ??? C ??? : ?? ??? Jun 19, 2025 am 12:25 AM

C polymorphismisuniqueduetoitscompikeofile-timeandruntimepolymorphism, forbothefficiency andfoxible.toharnesspowertylogly : 1) audesMartPointerSlikestd :: Quanior_PtrformemoryManagement, 2) ?? baseclasseshavevirtuctors, 3) ??

See all articles