当前位置:   article > 正文

io redirect_ioredirect

ioredirect

前言

和别人讨论问题,说到IO重定向。只有在低版本vs中, 替换_iob[]的方法。
win版,可以用freopen来实现IO重定向.
linux版,可以用dup2来实现IO重定向.
如果能使用API来进行IO重定向,那兼容性好多了. 代码实现也优雅的多:)

win版实验

// @file io_redirect.cpp
// @brief io redirect to file, env is vs2017

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)

#ifndef MAXBYTE
#define MAXBYTE 0xff
#endif // #ifndef MAXBYTE

#define LOG_FILE_PATH_NAME "d:\\my_log.txt"

FILE* g_fp_log_file = NULL;
FILE* g_fp_stdout_org = NULL;
FILE* g_fp_stdout_new = NULL;

void log_init();
void log_uninit();
int main_proc(int argc, char** argv);
void show_file_content(const char* psz_file_path_name);

int main(int argc, char** argv)
{
    printf(">> main\r\n");
    system("del d:\\my_log.txt");

    log_init();
    main_proc(argc, argv); // this print on file
    log_uninit();

    // this print on console
    printf(">> ---------- d:\\my_log.txt ----------\r\n");
    show_file_content(LOG_FILE_PATH_NAME);
    printf("<< ---------- d:\\my_log.txt ----------\r\n");
    printf("<< main\r\n");
    system("pause");

    /* run result on consle
    >> main
    >> log_init
    << log_uninit
    >> ---------- d:\my_log.txt ----------
    << log_init
    argc = 1
    argv[0] = D:\ls_RE\prj\study\linux_prj\printf_redirect\win\x64\Debug\printf_redirect.exe
    >> log_uninit
    << ---------- d:\my_log.txt ----------
    << main
    请按任意键继续. . .
    */

    return 0;
}

void log_init()
{
    printf(">> log_init\r\n");

    // 用替换_iob[]中的FILE*不通用,高版本vs没有_iob
    // printf use stdout as io handle
    // #define stdout (__acrt_iob_func(1))
    //  _ACRTIMP_ALT FILE* __cdecl __acrt_iob_func(unsigned _Ix);
    /*
        #ifndef _FILE_DEFINED
        #define _FILE_DEFINED
        typedef struct _iobuf
        {
        void* _Placeholder;
        } FILE;
        #endif
    */

    // 用freopen兼容性很好
    g_fp_stdout_org = stdout;
    g_fp_log_file = freopen(LOG_FILE_PATH_NAME, "a+", stdout);
    if (NULL == g_fp_log_file) {
        printf("error : can't io redirect stdout\r\n");
    }

    printf("<< log_init\r\n");
}

void log_uninit()
{
    printf(">> log_uninit\r\n");
    if (NULL != g_fp_log_file) {
        fflush(stdout);
        fclose(g_fp_log_file);
        g_fp_log_file = NULL;

        g_fp_stdout_new = freopen("CON", "w", stdout);
        if (NULL == g_fp_stdout_new) {
            printf("error : can't io redirect stdout\r\n");
        }
    }
    printf("<< log_uninit\r\n");
}

int main_proc(int argc, char** argv)
{
    int i = 0;

    printf("argc = %d\r\n", argc);
    for (i = 0; i < argc; i++) {
        printf("argv[%d] = %s\r\n", i, argv[i]);
    }

    return EXIT_SUCCESS;
}

void show_file_content(const char* psz_file_path_name)
{
    FILE* fp = NULL;
    char sz_buf[4096] = { '\0' };

    do {
        if (NULL == psz_file_path_name) {
            break;
        }

        fp = fopen(psz_file_path_name, "r");
        if (NULL == fp) {
            break;
        }

        do {
            memset(sz_buf, 0, sizeof(sz_buf));
            if (NULL == fgets(sz_buf, (int)sizeof(sz_buf), fp)) {
                break;
            }

            printf("%s", sz_buf);
        } while (1);
    } while (0);

    if (NULL != fp) {
        fclose(fp);
        fp = NULL;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146

linux版实验

// @file io_redirect.cpp
// @brief io redirect to file, env is debian7.5

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#ifndef MAXBYTE
#define MAXBYTE 0xff
#endif // #ifndef MAXBYTE

#define LOG_FILE_PATH_NAME "/var/log/my_log.txt"

int g_fd_stdout_dup = -1;
FILE* g_fp_log_file = NULL;

void log_init();
void log_uninit();
int main_proc(int argc, char** argv);
void show

int main(int argc, char** argv)
{
    printf(">> main\r\n");
    system("rm /var/log/my_log.txt");

    log_init();
    main_proc(argc, argv); // this print on file
    log_uninit();

    // this print on console
    printf(">> ---------- /var/log/my_log.txt ----------\r\n");
    system("cat /var/log/my_log.txt");
    printf("<< ---------- /var/log/my_log.txt ----------\r\n");
    printf("<< main\r\n");

    /* run result on consle
    >> main
    >> ---------- /var/log/my_log.txt ----------
    argc = 1
    argv[0] = ./case_io_redirect
    << ---------- /var/log/my_log.txt ----------
    << main
    */

    return 0;
}

void log_init()
{
    int fd_stdout = -1;
    int fd_log_file = -1;

    g_fp_log_file = fopen(LOG_FILE_PATH_NAME, "a+");
    if (NULL != g_fp_log_file) {
        if (-1 == g_fd_stdout_dup) {
            g_fd_stdout_dup = dup(STDOUT_FILENO);
        }

        fd_log_file = fileno(g_fp_log_file);
        fd_stdout = dup2(fd_log_file, STDOUT_FILENO);
    }
}
void log_uninit()
{
    if (NULL != g_fp_log_file) {
        fflush(stdout);
        fclose(g_fp_log_file);
        g_fp_log_file = NULL;
    }

    if (-1 != g_fd_stdout_dup) {
        dup2(g_fd_stdout_dup, STDOUT_FILENO);
        close(g_fd_stdout_dup);
        g_fd_stdout_dup = -1;
    }
}

int main_proc(int argc, char** argv)
{
    int i = 0;

    printf("argc = %d\r\n", argc);
    for (i = 0; i < argc; i++) {
        printf("argv[%d] = %s\r\n", i, argv[i]);
    }

    return EXIT_SUCCESS;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/721881
推荐阅读
相关标签
  

闽ICP备14008679号