이진 형식 로그 파일에서 CSV 형식 로그 파일로 데이터 변환
다음은 성능 도구에서 만든 카운터 로그 파일에서 쉼표로 구분된 형식(.csv)으로 데이터를 전송하는 예제입니다. 이 예제에서는 로컬 컴퓨터에서 수집된 프로세서 시간 카운터 데이터를 전송합니다. 다른 유형의 카운터 데이터를 지정하려면 szCounterPath 변수를 변경합니다. 수집된 카운터 데이터가 특정 컴퓨터에서 온 경우 경로에 컴퓨터 이름을 추가합니다(예: "\\\\<computername>\\Processor(0)\\% Processor Time").
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <pdh.h>
#include <pdhmsg.h>
#pragma comment(lib, "pdh.lib")
CONST PWSTR COUNTER_PATH = L"\\Processor(0)\\% Processor Time";
void DisplayCommandLineHelp(void)
{
wprintf(L"Syntax: convertlog <input file name> <output file name>\n"
L"\nThe input log file must be in the Perfmon format. The output\n"
L"log file will written in the CSV file format, so specify a .csv extension.");
}
void wmain(int argc, WCHAR **argv)
{
HQUERY hQuery = NULL;
HLOG hOutputLog = NULL;
HCOUNTER hCounter = NULL;
PDH_STATUS pdhStatus = ERROR_SUCCESS;
DWORD dwOutputLogType = PDH_LOG_TYPE_CSV;
if (3 != argc)
{
DisplayCommandLineHelp();
goto cleanup;
}
// Create the query object using the input log file.
pdhStatus = PdhOpenQuery(argv[1], 0, &hQuery);
if (ERROR_SUCCESS != pdhStatus)
{
wprintf(L"PdhOpenQuery failed with 0x%x\n", pdhStatus);
goto cleanup;
}
// Add the counter to the query object; identifies the counter
// records from the log file that you are going to relog to
// the new log file.
pdhStatus = PdhAddCounter(hQuery,
COUNTER_PATH,
0,
&hCounter);
if (ERROR_SUCCESS != pdhStatus)
{
wprintf(L"PdhAddCounter failed with 0x%x\n", pdhStatus);
goto cleanup;
}
// Create and open the output log file.
pdhStatus = PdhOpenLog(argv[2],
PDH_LOG_WRITE_ACCESS |
PDH_LOG_CREATE_ALWAYS,
&dwOutputLogType,
hQuery,
0,
NULL,
&hOutputLog);
if (ERROR_SUCCESS != pdhStatus)
{
wprintf(L"PdhOpenLog failed with 0x%x\n", pdhStatus);
goto cleanup;
}
// Transfer the log records from the input file to the output file.
while (ERROR_SUCCESS == pdhStatus)
{
pdhStatus = PdhUpdateLog(hOutputLog, NULL);
}
if (PDH_NO_MORE_DATA != pdhStatus)
{
wprintf(L"PdhUpdateLog failed with 0x%x\n", pdhStatus);
}
cleanup:
// Close the output log file.
if (hOutputLog)
PdhCloseLog(hOutputLog, 0);
// Close the query object and input log file.
if (hQuery)
PdhCloseQuery(hQuery);
}