[关闭]
@zhengyuhong 2017-02-10T07:26:56.000000Z 字数 3959 阅读 1529

public/common

code


streaming_log - Print log to std::ostreams

  1. CONFIGS('public/common@ci-base')

base/logging.h

可与comlog, ullog混用的流式日志,接口和glog一致,你再也不会想用comlog那套接口了!

  1. LOG(FATAL) << "Fatal error occurred! contexts=" << ...;
  2. LOG(WARNING) << "Unusual thing happened ..." << ...;
  3. LOG(TRACE) << "Something just took place..." << ...;
  4. LOG(TRACE) << "Items:" << noflush;
  5. LOG_IF(NOTICE, n > 10) << "This log will only be printed when n > 10";
  6. PLOG(FATAL) << "Fail to call function setting errno";
  7. VLOG(1) << "verbose log tier 1";
  8. CHECK_GT(1, 2) << "1 can't be greater than 2";
  9. // public/common >= r32401支持限制打印频率。
  10. LOG_EVERY_SECOND(INFO) << "High-frequent logs";
  11. LOG_EVERY_N(ERROR, 10) << "High-frequent logs";
  12. LOG_FIRST_N(INFO, 20) << "Logs that prints for at most 20 times";
  13. LOG_ONCE(WARNING) << "Logs that only prints once";
  1. // logging默认重定向至comlog,要配置comlog的话,要额外include comlog_sink.h
  2. #include <base/comlog_sink.h>
  3. // 从./conf/log.conf读取comlog的配置。SetupFromConfig是我们提供的封装函数,不用像com_loadlog那样区分path和file。
  4. if (logging::ComlogSink::GetInstance()->SetupFromConfig("conf/log.conf") != 0) {
  5. LOG(ERROR) << "Fail to setup comlog from conf/log.conf";
  6. return -1;
  7. }

more detail

base/containers/flat_map.h

  1. base::FlatMap<int, std::string> map;
  2. // bucket_count: initial count of buckets, big enough to avoid resize.
  3. // load_factor: element_count * 100 / bucket_count, 80 as default.
  4. map.init(bucket_count, load_factor);
  5. map.insert(10, "hello");
  6. map[20] = "world";
  7. std::string* value = map.seek(20);
  8. if (value != NULL) {
  9. LOG(TRACE) << "Got the value=" << *value;
  10. } else {
  11. LOG(FATAL) << "Impossible, we've just put the value in";
  12. }
  13. LOG(TRACE) << "All elements of the map: " << noflush;
  14. for (base::FlatMap<int, std::string>::const_iterator it = map.begin(); it != map.end(); ++it) {
  15. LOG(TRACE) << it->first << "->" << it->second << " ";
  16. }
  17. LOG(TRACE);

FlatMap可能是最快的哈希表,但当value较大时它需要更多的内存,它最适合作为检索过程中需要极快查找的小字典。
原理:把开链桶中第一个节点的内容直接放桶内。由于在实践中,大部分桶没有冲突或冲突较少,所以大部分操作只需要一次内存跳转:通过哈希值访问对应的桶。桶内两个及以上元素仍存放在链表中,由于桶之间彼此独立,一个桶的冲突不会影响其他桶,性能很稳定。

more detail

base/file_watcher.h

检查文件的时间戳变化

  1. namespace base {
  2. class FileWatcher {
  3. public:
  4. enum Change {
  5. DELETED = -1,
  6. UNCHANGED = 0,
  7. UPDATED = 1,
  8. CREATED = 2,
  9. };
  10. typedef int64_t Timestamp;
  11. FileWatcher();
  12. // Watch file at `file_path', must be called before calling other methods.
  13. // Returns 0 on success, -1 otherwise.
  14. int init(const char* file_path);
  15. // Let check_and_consume returns CREATE when file_path already exists.
  16. int init_from_not_exist(const char* file_path);
  17. // Check and consume change of the watched file. Write `last_timestamp'
  18. // if it's not NULL.
  19. // Returns:
  20. // CREATE the file is created since last call to this method.
  21. // UPDATED the file is modified since last call.
  22. // UNCHANGED the file has no change since last call.
  23. // DELETED the file was deleted since last call.
  24. // Note: If the file is updated too frequently, this method may return
  25. // UNCHANGED due to precision of stat(2) and the file system. If the file
  26. // is created and deleted too frequently, the event may not be detected.
  27. Change check_and_consume(Timestamp* last_timestamp = NULL);
  28. // Set internal timestamp. User can use this method to make
  29. // check_and_consume() replay the change.
  30. void restore(Timestamp timestamp);
  31. // Get path of watched file
  32. const char* filepath() const { return _file_path.c_str(); }
  33. private:
  34. Change check(Timestamp* new_timestamp) const;
  35. std::string _file_path;
  36. Timestamp _last_ts;
  37. };
  38. } // namespace base

base/timer/timer.h

Sample RepeatingTimer usage:

  1. class MyClass {
  2. public:
  3. void StartDoingStuff() {
  4. timer_.Start(FROM_HERE, TimeDelta::FromSeconds(1),
  5. this, &MyClass::DoStuff);
  6. //FROM_HERE see in base/location.h
  7. //TimeDelta see in base/time/time.h
  8. }
  9. void StopDoingStuff() {
  10. timer_.Stop();
  11. }
  12. private:
  13. void DoStuff() {
  14. // This method is called every second to do stuff.
  15. //...
  16. }
  17. base::RepeatingTimer<MyClass> timer_;
  18. };

base/file_watcher.h

  1. Example:
  2. FileWatcher fw;
  3. fw.init("to_be_watched_file");
  4. //....
  5. if (fw.check_and_consume() > 0) {
  6. // the file is created or updated
  7. //......
  8. }

base/timer/elapsed_timer.h

计时器

  1. class BASE_EXPORT ElapsedTimer {
  2. public:
  3. ElapsedTimer();
  4. virtual ~ElapsedTimer() {}
  5. // Returns the time elapsed since object construction.
  6. virtual TimeDelta Elapsed() const;
  7. private:
  8. TimeTicks begin_;
  9. DISALLOW_COPY_AND_ASSIGN(ElapsedTimer);
  10. };

base/md5.h

  1. struct MD5Digest {
  2. unsigned char a[16];
  3. };
  4. //Computes the MD5 sum of the given data buffer with the given length.
  5. BASE_EXPORT void MD5Sum(const void* data, size_t length, MD5Digest* digest);

base/sha1.h

  1. BASE_EXPORT std::string SHA1HashString(const std::string& str);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注