@zhengyuhong
2017-02-10T07:26:56.000000Z
字数 3959
阅读 1529
code
streaming_log - Print log to std::ostreams
CONFIGS('public/common@ci-base')
可与comlog, ullog混用的流式日志,接口和glog一致,你再也不会想用comlog那套接口了!
LOG(FATAL) << "Fatal error occurred! contexts=" << ...;LOG(WARNING) << "Unusual thing happened ..." << ...;LOG(TRACE) << "Something just took place..." << ...;LOG(TRACE) << "Items:" << noflush;LOG_IF(NOTICE, n > 10) << "This log will only be printed when n > 10";PLOG(FATAL) << "Fail to call function setting errno";VLOG(1) << "verbose log tier 1";CHECK_GT(1, 2) << "1 can't be greater than 2";// public/common >= r32401支持限制打印频率。LOG_EVERY_SECOND(INFO) << "High-frequent logs";LOG_EVERY_N(ERROR, 10) << "High-frequent logs";LOG_FIRST_N(INFO, 20) << "Logs that prints for at most 20 times";LOG_ONCE(WARNING) << "Logs that only prints once";
// logging默认重定向至comlog,要配置comlog的话,要额外include comlog_sink.h#include <base/comlog_sink.h>// 从./conf/log.conf读取comlog的配置。SetupFromConfig是我们提供的封装函数,不用像com_loadlog那样区分path和file。if (logging::ComlogSink::GetInstance()->SetupFromConfig("conf/log.conf") != 0) {LOG(ERROR) << "Fail to setup comlog from conf/log.conf";return -1;}
base::FlatMap<int, std::string> map;// bucket_count: initial count of buckets, big enough to avoid resize.// load_factor: element_count * 100 / bucket_count, 80 as default.map.init(bucket_count, load_factor);map.insert(10, "hello");map[20] = "world";std::string* value = map.seek(20);if (value != NULL) {LOG(TRACE) << "Got the value=" << *value;} else {LOG(FATAL) << "Impossible, we've just put the value in";}LOG(TRACE) << "All elements of the map: " << noflush;for (base::FlatMap<int, std::string>::const_iterator it = map.begin(); it != map.end(); ++it) {LOG(TRACE) << it->first << "->" << it->second << " ";}LOG(TRACE);
FlatMap可能是最快的哈希表,但当value较大时它需要更多的内存,它最适合作为检索过程中需要极快查找的小字典。
原理:把开链桶中第一个节点的内容直接放桶内。由于在实践中,大部分桶没有冲突或冲突较少,所以大部分操作只需要一次内存跳转:通过哈希值访问对应的桶。桶内两个及以上元素仍存放在链表中,由于桶之间彼此独立,一个桶的冲突不会影响其他桶,性能很稳定。
检查文件的时间戳变化
namespace base {class FileWatcher {public:enum Change {DELETED = -1,UNCHANGED = 0,UPDATED = 1,CREATED = 2,};typedef int64_t Timestamp;FileWatcher();// Watch file at `file_path', must be called before calling other methods.// Returns 0 on success, -1 otherwise.int init(const char* file_path);// Let check_and_consume returns CREATE when file_path already exists.int init_from_not_exist(const char* file_path);// Check and consume change of the watched file. Write `last_timestamp'// if it's not NULL.// Returns:// CREATE the file is created since last call to this method.// UPDATED the file is modified since last call.// UNCHANGED the file has no change since last call.// DELETED the file was deleted since last call.// Note: If the file is updated too frequently, this method may return// UNCHANGED due to precision of stat(2) and the file system. If the file// is created and deleted too frequently, the event may not be detected.Change check_and_consume(Timestamp* last_timestamp = NULL);// Set internal timestamp. User can use this method to make// check_and_consume() replay the change.void restore(Timestamp timestamp);// Get path of watched fileconst char* filepath() const { return _file_path.c_str(); }private:Change check(Timestamp* new_timestamp) const;std::string _file_path;Timestamp _last_ts;};} // namespace base
Sample RepeatingTimer usage:
class MyClass {public:void StartDoingStuff() {timer_.Start(FROM_HERE, TimeDelta::FromSeconds(1),this, &MyClass::DoStuff);//FROM_HERE see in base/location.h//TimeDelta see in base/time/time.h}void StopDoingStuff() {timer_.Stop();}private:void DoStuff() {// This method is called every second to do stuff.//...}base::RepeatingTimer<MyClass> timer_;};
Example:FileWatcher fw;fw.init("to_be_watched_file");//....if (fw.check_and_consume() > 0) {// the file is created or updated//......}
计时器
class BASE_EXPORT ElapsedTimer {public:ElapsedTimer();virtual ~ElapsedTimer() {}// Returns the time elapsed since object construction.virtual TimeDelta Elapsed() const;private:TimeTicks begin_;DISALLOW_COPY_AND_ASSIGN(ElapsedTimer);};
struct MD5Digest {unsigned char a[16];};//Computes the MD5 sum of the given data buffer with the given length.BASE_EXPORT void MD5Sum(const void* data, size_t length, MD5Digest* digest);
BASE_EXPORT std::string SHA1HashString(const std::string& str);