1 /*
2 * Copyright (c) 2021-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 #define HST_LOG_TAG "DumpBuffer"
16
17 #include "dump_buffer.h"
18 #include <cstdio>
19 #ifdef _WIN32
20 #include <direct.h>
21 #else
22 #include <unistd.h>
23 #endif
24 #include <dirent.h>
25 #include "foundation/log.h"
26 #include "foundation/osal/filesystem/file_system.h"
27
28 namespace OHOS {
29 namespace Media {
30 namespace Pipeline {
31 #ifdef _WIN32
32 #define DUMP_FILE_DIR ""
33 #endif
34 // Specify the dump file dir in BUILD.gn
35 #ifndef DUMP_FILE_DIR
36 #define DUMP_FILE_DIR "/data/local/tmp"
37 #endif
38
GetDumpFileDir()39 std::string GetDumpFileDir()
40 {
41 auto rootDir = std::string(DUMP_FILE_DIR);
42 return rootDir.empty() ? "histreamer_dump_files/" : rootDir + "/histreamer_dump_files/";
43 }
44
DumpBufferToFile(const std::string & fileName,const std::shared_ptr<Plugin::Buffer> & buffer)45 void DumpBufferToFile(const std::string& fileName, const std::shared_ptr<Plugin::Buffer>& buffer)
46 {
47 size_t bufferSize = buffer->GetMemory()->GetSize();
48 FALSE_RETURN(bufferSize != 0);
49
50 std::string filePath = GetDumpFileDir() + fileName;
51 auto filePtr = fopen(filePath.c_str(), "ab+");
52 FALSE_RETURN_MSG(filePtr != nullptr, "Open file(" PUBLIC_LOG_S ") failed(" PUBLIC_LOG_S ").", filePath.c_str(),
53 strerror(errno));
54 (void)fwrite(reinterpret_cast<const char*>(buffer->GetMemory()->GetReadOnlyData()),
55 1, bufferSize, filePtr);
56 (void)fclose(filePtr);
57 }
58
PrepareDumpDir()59 void PrepareDumpDir()
60 {
61 std::string dumpDir = GetDumpFileDir();
62 const char* fileDir = dumpDir.c_str();
63 if (access(fileDir, 0) == 0) { // 目录存在
64 OSAL::FileSystem::RemoveFilesInDir(fileDir);
65 } else {
66 (void)OSAL::FileSystem::MakeMultipleDir(fileDir);
67 }
68 }
69
DumpBufferToLog(const char * desc,const std::shared_ptr<Plugin::Buffer> & buffer,uint64_t offset,size_t dumpSize)70 void DumpBufferToLog(const char* desc, const std::shared_ptr<Plugin::Buffer>& buffer, uint64_t offset, size_t dumpSize)
71 {
72 FALSE_RETURN_MSG(buffer && (!buffer->IsEmpty()), PUBLIC_LOG_S " Buffer(null or empty)", desc);
73 size_t bufferSize = buffer->GetMemory()->GetSize();
74 size_t realDumpSize = std::min(dumpSize, bufferSize);
75 realDumpSize = std::min(realDumpSize, static_cast<size_t>(DUMP_BUFFER2LOG_SIZE)); // max DUMP_BUFFER2LOG_SIZE bytes
76 char tmpStr[2 * DUMP_BUFFER2LOG_SIZE + 10] = {0}; // 字符串长度是打印的buffer长度的2倍 + 1 (字符串结束符)
77 char* dstPtr = tmpStr;
78 int len;
79 const uint8_t* p = buffer->GetMemory()->GetReadOnlyData();
80 for (size_t i = 0; i < realDumpSize; i++) {
81 len = snprintf_s(dstPtr, 3, 2, "%02x", p[i]); // max write 3 bytes, string len 2
82 FALSE_RETURN_MSG(len > 0 && len <= 2, "snprintf_s returned unexpected value " PUBLIC_LOG_D32, len); // max len 2
83 dstPtr += len;
84 }
85 MEDIA_LOG_I(PUBLIC_LOG_S " Buffer(offset " PUBLIC_LOG_D64 ", size " PUBLIC_LOG_ZU ", capacity "
86 PUBLIC_LOG_ZU ") : " PUBLIC_LOG_S, desc, offset, bufferSize, buffer->GetMemory()->GetCapacity(), tmpStr);
87 }
88 } // Pipeline
89 } // Media
90 } // OHOS