1 /*
2 * Copyright (c) 2021 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 #include "log_table.h"
17
18 namespace SysTuning {
19 namespace TraceStreamer {
20 namespace {
21 enum Index { SEQ = 0, TS, PID, TID, LEVEL, TAG, CONTEXT, ORIGINTS };
22 }
LogTable(const TraceDataCache * dataCache)23 LogTable::LogTable(const TraceDataCache* dataCache) : TableBase(dataCache)
24 {
25 tableColumn_.push_back(TableBase::ColumnInfo("seq", "INTEGER"));
26 tableColumn_.push_back(TableBase::ColumnInfo("ts", "INTEGER"));
27 tableColumn_.push_back(TableBase::ColumnInfo("pid", "INTEGER"));
28 tableColumn_.push_back(TableBase::ColumnInfo("tid", "INTEGER"));
29 tableColumn_.push_back(TableBase::ColumnInfo("level", "TEXT"));
30 tableColumn_.push_back(TableBase::ColumnInfo("tag", "TEXT"));
31 tableColumn_.push_back(TableBase::ColumnInfo("context", "TEXT"));
32 tableColumn_.push_back(TableBase::ColumnInfo("origints", "INTEGER"));
33 tablePriKey_.push_back("ts");
34 }
35
~LogTable()36 LogTable::~LogTable() {}
37
CreateCursor()38 std::unique_ptr<TableBase::Cursor> LogTable::CreateCursor()
39 {
40 return std::make_unique<Cursor>(dataCache_, this);
41 }
42
Cursor(const TraceDataCache * dataCache,TableBase * table)43 LogTable::Cursor::Cursor(const TraceDataCache* dataCache, TableBase* table)
44 : TableBase::Cursor(dataCache, table, static_cast<uint32_t>(dataCache->GetConstHilogData().Size())),
45 logInfoObj_(dataCache->GetConstHilogData())
46 {
47 }
48
~Cursor()49 LogTable::Cursor::~Cursor() {}
50
Column(int column) const51 int LogTable::Cursor::Column(int column) const
52 {
53 switch (column) {
54 case SEQ:
55 sqlite3_result_int64(context_, static_cast<int64_t>(logInfoObj_.HilogLineSeqs()[CurrentRow()]));
56 break;
57 case TS:
58 sqlite3_result_int64(context_, static_cast<int64_t>(logInfoObj_.TimeStamData()[CurrentRow()]));
59 break;
60 case PID: {
61 sqlite3_result_int64(context_, static_cast<int64_t>(logInfoObj_.Pids()[CurrentRow()]));
62 break;
63 }
64 case TID:
65 sqlite3_result_int64(context_, static_cast<int64_t>(logInfoObj_.Tids()[CurrentRow()]));
66 break;
67 case LEVEL: {
68 if (logInfoObj_.Levels()[CurrentRow()] != INVALID_UINT64) {
69 auto levelDataIndex = static_cast<size_t>(logInfoObj_.Levels()[CurrentRow()]);
70 sqlite3_result_text(context_, dataCache_->GetDataFromDict(levelDataIndex).c_str(), STR_DEFAULT_LEN,
71 nullptr);
72 }
73 break;
74 }
75 case TAG: {
76 if (logInfoObj_.Tags()[CurrentRow()] != INVALID_UINT64) {
77 auto tagDataIndex = static_cast<size_t>(logInfoObj_.Tags()[CurrentRow()]);
78 sqlite3_result_text(context_, dataCache_->GetDataFromDict(tagDataIndex).c_str(), STR_DEFAULT_LEN,
79 nullptr);
80 }
81 break;
82 }
83 case CONTEXT: {
84 if (logInfoObj_.Contexts()[CurrentRow()] != INVALID_UINT64) {
85 auto contextDataIndex = static_cast<size_t>(logInfoObj_.Contexts()[CurrentRow()]);
86 sqlite3_result_text(context_, dataCache_->GetDataFromDict(contextDataIndex).c_str(), STR_DEFAULT_LEN,
87 nullptr);
88 }
89 break;
90 }
91 case ORIGINTS: {
92 sqlite3_result_int64(context_, static_cast<int64_t>(logInfoObj_.OriginTimeStamData()[CurrentRow()]));
93 break;
94 }
95 default:
96 TS_LOGF("Unregistered column : %d", column);
97 break;
98 }
99 return SQLITE_OK;
100 }
101 } // namespace TraceStreamer
102 } // namespace SysTuning
103