source: trunk/Utilities/LogStdOutput/LogStdOutput.cpp @ 937

Last change on this file since 937 was 517, checked in by roman, 9 years ago
File size: 2.0 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2015
3// Created by Roman Ryltsov roman@alax.info
4//
5// A permission to use the source code is granted as long as reference to
6// source website http://alax.info is retained.
7
8#include "stdafx.h"
9#include <iostream>
10
11CEvent g_TerminationEvent;
12
13BOOL InternalHandlerRoutine(DWORD nType)
14{
15        _W(g_TerminationEvent.Set());
16        return TRUE;
17}
18BOOL WINAPI HandlerRoutine(DWORD nType)
19{
20        return InternalHandlerRoutine(nType);
21}
22
23int _tmain(int argc, _TCHAR* argv[])
24{
25        // NOTE: generate 2>nul | logstdoutput filename.ext
26        _ATLTRY
27        {
28                if(argc < 2)
29                {
30                        std::cerr << "Syntax: LogStdOutput <path>" << std::endl;
31                        return 1;
32                }
33                __E(g_TerminationEvent.Create(NULL, TRUE, FALSE, NULL));
34                __E(SetConsoleCtrlHandler(&HandlerRoutine, TRUE));
35                const HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
36                const HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
37                __D(hInput && hOutput, E_UNNAMED);
38                CAtlFile File;
39                __C(File.Create(argv[1], GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS));
40                HANDLE phObjects[] = { g_TerminationEvent, hInput };
41                static const SIZE_T g_nDataCapacity = 256 << 10; // 256 KB
42                CHeapPtr<BYTE> pnData;
43                __D(pnData.Allocate(g_nDataCapacity), E_OUTOFMEMORY);
44                for(; ; )
45                {
46                        const DWORD nWaitResult = WaitForMultipleObjects(DIM(phObjects), phObjects, FALSE, INFINITE);
47                        _Z5_WAITRESULT(nWaitResult);
48                        if(nWaitResult != WAIT_OBJECT_0 + 1) // hInput
49                                break; // Termination?
50                        DWORD nDataSize = 0;
51                        __E(ReadFile(hInput, pnData, (DWORD) g_nDataCapacity, &nDataSize, NULL));
52                        if(!nDataSize)
53                                break;
54                        // NOTE: nWriteDataSize is mandatory
55                        //       https://social.msdn.microsoft.com/Forums/vstudio/en-US/e519816e-57c6-4b5d-9269-3ad1494eedbf/writefile-on-a-console-handle-always-fails-why?forum=vcgeneral
56                        DWORD nWriteDataSize = 0;
57                        __E(WriteFile(hOutput, pnData, nDataSize, &nWriteDataSize, NULL));
58                        __C(File.Write(pnData, nDataSize));
59                }
60        }
61        _ATLCATCH(Exception)
62        {
63                return Exception;
64        }
65        return 0;
66}
Note: See TracBrowser for help on using the repository browser.