xref: /test/ostest/wukong/report/include/csv_utils.h (revision a69a01cd)
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef TEST_WUKONG_CSV_UTILS_H
17#define TEST_WUKONG_CSV_UTILS_H
18
19#include <fstream>
20#include <iostream>
21
22#include "wukong_define.h"
23
24namespace OHOS {
25namespace WuKong {
26class CsvUtils {
27public:
28    struct OneLineData {
29        std::string domain;
30        std::string name;
31        std::string type;
32        uint64_t time;
33        std::string timeZone;
34        uint64_t pid;
35        uint64_t tid;
36        uint64_t uid;
37        uint64_t traceId;
38        uint64_t spanId;
39        uint64_t pspanId;
40        uint64_t traceFlag;
41    };
42
43    static void WriteHeader(std::ofstream &csvFile)
44    {
45        csvFile << "Domain" << ',';
46        csvFile << "EventName" << ',';
47        csvFile << "Type" << ',';
48        csvFile << "Time" << ',';
49        csvFile << "TimeZone" << ',';
50        csvFile << "ProcessId" << ',';
51        csvFile << "ThreadId" << ',';
52        csvFile << "UserId" << ',';
53        csvFile << "TraceId" << ',';
54        csvFile << "SpanId" << ',';
55        csvFile << "PSpanid" << ',';
56        csvFile << "TraceFlag" << std::endl;
57    }
58
59    static void WriteOneLine(std::ofstream &csvFile, const OneLineData &data)
60    {
61        if (!csvFile.is_open()) {
62            return;
63        }
64        csvFile << data.domain << ',';
65        csvFile << data.name << ',';
66        csvFile << data.type << ',';
67        csvFile << data.time << ',';
68        csvFile << data.timeZone << ',';
69        csvFile << data.pid << ',';
70        csvFile << data.tid << ',';
71        csvFile << data.uid << ',';
72        csvFile << data.traceId << ',';
73        csvFile << data.spanId << ',';
74        csvFile << data.pspanId << ',';
75        csvFile << data.traceFlag << std::endl;
76    }
77};
78}  // namespace WuKong
79}  // namespace OHOS
80
81#endif  // WUKONG_WUKONG_CSV_UTILS_H
82