2 * Copyright 2012 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef FOLLY_LOGGING_H_
18 #define FOLLY_LOGGING_H_
21 #include <glog/logging.h>
23 #ifndef FB_LOG_EVERY_MS
25 * Issues a LOG(severity) no more often that every
26 * milliseconds. Example:
28 * FB_LOG_EVERY_MS(INFO, 10000) << "At least ten seconds passed"
29 * " since you last saw this.";
31 * The implementation uses for statements to introduce variables in
32 * a nice way that doesn't mess surrounding statements.
34 #define FB_LOG_EVERY_MS(severity, milliseconds) \
35 for (bool LOG_EVERY_MS_once = true; LOG_EVERY_MS_once; ) \
36 for (const ::clock_t LOG_EVERY_MS_now = ::clock(); LOG_EVERY_MS_once; ) \
37 for (static ::clock_t LOG_EVERY_MS_last; LOG_EVERY_MS_once; \
38 LOG_EVERY_MS_once = false) \
39 if (1000 * (LOG_EVERY_MS_now - LOG_EVERY_MS_last) < \
40 (milliseconds) * CLOCKS_PER_SEC) {} \
42 (LOG_EVERY_MS_last = LOG_EVERY_MS_now, LOG(severity))
46 #endif // FOLLY_LOGGING_H_