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 #ifndef HILOG_COMPRESS_H 16 #define HILOG_COMPRESS_H 17 18 #include "hilog_common.h" 19 #include <iostream> 20 #ifdef USING_ZSTD_COMPRESS 21 #define ZSTD_STATIC_LINKING_ONLY 22 #include "include/common.h" 23 #include "zstd.h" 24 #endif 25 #include <zlib.h> 26 27 namespace OHOS { 28 namespace HiviewDFX { 29 typedef struct { 30 char content[MAX_PERSISTER_BUFFER_SIZE]; 31 uint32_t offset; 32 } LogPersisterBuffer; 33 34 35 class LogCompress { 36 public: 37 LogCompress() = default; 38 virtual ~LogCompress() = default; 39 virtual int Compress(const LogPersisterBuffer &inBuffer, LogPersisterBuffer &compressBuffer) = 0; 40 }; 41 42 class NoneCompress : public LogCompress { 43 public: 44 int Compress(const LogPersisterBuffer &inBuffer, LogPersisterBuffer &compressBuffer) override; 45 }; 46 47 class ZlibCompress : public LogCompress { 48 public: 49 int Compress(const LogPersisterBuffer &inBuffer, LogPersisterBuffer &compressBuffer) override; 50 private: 51 static const uint16_t CHUNK = 16384; 52 char buffIn[CHUNK] = {0}; 53 char buffOut[CHUNK] = {0}; 54 55 z_stream cStream; 56 }; 57 58 class ZstdCompress : public LogCompress { 59 public: 60 int Compress(const LogPersisterBuffer &inBuffer, LogPersisterBuffer &compressBuffer) override; 61 private: 62 #ifdef USING_ZSTD_COMPRESS 63 static const uint16_t CHUNK = 16384; 64 char buffIn[CHUNK] = {0}; 65 char buffOut[CHUNK] = {0}; 66 ZSTD_CCtx* cctx; 67 #endif 68 }; 69 } 70 } 71 #endif /* HILOG_COMPRESS_H */ 72