TaskEvent#seq = TimeUtil::GetNanoTime();
namespace TimeUtil {
uint64_t GetNanoTime()
{
auto nanoNow = std::chrono::steady_clock::now().time_since_epoch();
return nanoNow.count();
}
auto task = [this, sysEvent]() {
HIVIEW_LOGD("event time:%{public}llu jsonExtraInfo is %{public}s", TimeUtil::GetMilliseconds(),
sysEvent->jsonExtraInfo_.c_str());
std::string SysEvent::GetEventValue(const std::string& key)
{
if (jsonExtraInfo_.empty() || key.empty()) {
return "";
}
std::string targetStr = "\"" + key + "\":\"";
size_t startPos = jsonExtraInfo_.find(targetStr);
if (startPos == std::string::npos) {
return "";
}
startPos += targetStr.size();
size_t endPos = startPos;
while (endPos < jsonExtraInfo_.size()) {
if (jsonExtraInfo_[endPos] == '\"') {
std::string value = jsonExtraInfo_.substr(startPos, endPos - startPos);
if (!value.empty()) {
SetValue(key, value);
}
return value;
}
if (jsonExtraInfo_[endPos] == '\\' && endPos < (jsonExtraInfo_.size() - 1)) { // 1: for '"'
endPos += 2; // 2: for '\' and '"'
continue;
}
endPos++;
}
return "";
}
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <source_file> <destination_directory>\n";
return 1;
}
```csharp
const char* source_file = argv[1];
const char* destination_directory = argv[2];
int source_fd = open(source_file, O_RDONLY);
if (source_fd == -1) {
std::cerr << "Failed to open source file: " << source_file << '\n';
return 1;
}
off_t file_size = lseek(source_fd, 0, SEEK_END);
lseek(source_fd, 0, SEEK_SET);
int destination_fd = open(destination_directory, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (destination_fd == -1) {
std::cerr << "Failed to open destination directory: " << destination_directory << '\n';
close(source_fd);
return 1;
}
if (sendfile(destination_fd, source_fd, 0, file_size) == -1) {
std::cerr << "Failed to copy file: " << source_file << '\n';
close(source_fd);
close(destination_fd);
return 1;
}
close(source_fd);
close(destination_fd);
return 0;
}
这个程序使用了 open() 函数打开源文件和目标目录,并使用 sendfile() 函数将源文件的内容传输到目标文件中,实现了文件的零拷贝。在传输过程中,源文件和目标文件之间实现了内核级别的数据传输,因此不会创建中间缓冲区,从而避免了数据的多次复制,提高了文件传输的效率。
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("example.txt");
if (!file.is_open()) {
std::cerr << "Failed to open file\n";
return 1;
}
file << "Hello, world!\n";
file << "This is an example file created and written by C++\n";
file.close();
return 0;
}