comlog_sink.cc 13 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2015 Baidu, Inc.
gejun's avatar
gejun committed
2 3 4 5 6 7 8 9 10 11 12 13 14
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

gejun's avatar
gejun committed
15 16 17 18
// Author: Ge,Jun (gejun@baidu.com)
// Date: Mon Jul 20 12:39:39 CST 2015

#include <com_log.h>
19 20 21 22 23 24
#include "butil/memory/singleton.h"
#include "butil/comlog_sink.h"
#include "butil/files/file_path.h"
#include "butil/fd_guard.h"
#include "butil/file_util.h"
#include "butil/endpoint.h"
gejun's avatar
gejun committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

namespace logging {
DECLARE_bool(log_year);
DECLARE_bool(log_hostname);

struct ComlogLayoutOptions {
    ComlogLayoutOptions() : shorter_log_level(true) {}
    
    bool shorter_log_level;
};

class ComlogLayout : public comspace::Layout {
public:
    explicit ComlogLayout(const ComlogLayoutOptions* options);
    ~ComlogLayout();
    int format(comspace::Event *evt);
private:
    ComlogLayoutOptions _options;
};

ComlogLayout::ComlogLayout(const ComlogLayoutOptions* options) {
    if (options) {
        _options = *options;
    }
}

ComlogLayout::~ComlogLayout() {
}

// Override Layout::format to have shorter prefixes. Patterns are just ignored.
int ComlogLayout::format(comspace::Event *evt) {
    const int bufsize = evt->_render_msgbuf_size;
    char* const buf = evt->_render_msgbuf;
    if (bufsize < 2){
        return -1;
    }

    time_t t = evt->_print_time.tv_sec;
    struct tm local_tm = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL};
#if _MSC_VER >= 1400
    localtime_s(&local_tm, &t);
#else
    localtime_r(&t, &local_tm);
#endif
    int len = 0;
    if (_options.shorter_log_level) {
        buf[len++] = *comspace::getLogName(evt->_log_level);
    } else {
        const char* const name = comspace::getLogName(evt->_log_level);
        int cp_len = std::min(bufsize - len, (int)strlen(name));
        memcpy(buf + len, name, cp_len);
        len += cp_len;
        if (len < bufsize - 1) {
            buf[len++] = ' ';
        }
    }
    if (len < bufsize - 1) {
        int ret = 0;
        if (FLAGS_log_year) {
            ret = snprintf(buf + len, bufsize - len,
                           "%04d%02d%02d %02d:%02d:%02d.%06d %5u ",
                           local_tm.tm_year + 1900,
                           local_tm.tm_mon + 1,
                           local_tm.tm_mday,
                           local_tm.tm_hour,
                           local_tm.tm_min,
                           local_tm.tm_sec,
                           (int)evt->_print_time.tv_usec,
                           (unsigned int)evt->_thread_id);
        } else {
            ret = snprintf(buf + len, bufsize - len,
                           "%02d%02d %02d:%02d:%02d.%06d %5u ",
                           local_tm.tm_mon + 1,
                           local_tm.tm_mday,
                           local_tm.tm_hour,
                           local_tm.tm_min,
                           local_tm.tm_sec,
                           (int)evt->_print_time.tv_usec,
                           (unsigned int)evt->_thread_id);
        }
        if (ret >= 0) {
            len += ret;
        } else {
            // older glibc may return negative which means the buffer is full.
            len = bufsize;
        }
    }
    if (len > 0 && len < bufsize - 1) {  // not truncated.
        // Although it's very stupid, we have to copy the message again due
        // to the design of comlog.
        int cp_len = std::min(bufsize - len, evt->_msgbuf_len);
        memcpy(buf + len, evt->_msgbuf, cp_len);
        len += cp_len;
    }
    if (len >= bufsize - 1) {
        len = bufsize - 2;
    }
    buf[len++] = '\n';
    buf[len] = 0;
    evt->_render_msgbuf_len = len;
    return 0;
}

ComlogSink* ComlogSink::GetInstance() {
    return Singleton<ComlogSink, LeakySingletonTraits<ComlogSink> >::get();
}

ComlogSinkOptions::ComlogSinkOptions()
    : async(false)
    , shorter_log_level(true)
    , log_dir("log")
    , max_log_length(2048)
    , print_vlog_as_warning(true)
    , split_type(COMLOG_SPLIT_TRUNCT)
    , cut_size_megabytes(2048)
    , quota_size(0)
    , cut_interval_minutes(60)
    , quota_day(0)
    , quota_hour(0)
    , quota_min(0)
    , enable_wf_device(false) {
}

ComlogSink::ComlogSink() 
    : _init(false), _dev(NULL) {
}

int ComlogSink::SetupFromConfig(const std::string& conf_path_str) {
    Unload();
154
    butil::FilePath path(conf_path_str);
gejun's avatar
gejun committed
155 156 157 158 159 160 161 162 163 164 165
    if (com_loadlog(path.DirName().value().c_str(),
                    path.BaseName().value().c_str()) != 0) {
        LOG(ERROR) << "Fail to create ComlogSink from `" << conf_path_str << "'";
        return -1;
    }
    _init = true;
    return 0;
}

// This is definitely linux specific.
static std::string GetProcessName() {
166
    butil::fd_guard fd(open("/proc/self/cmdline", O_RDONLY));
gejun's avatar
gejun committed
167 168 169 170 171 172 173 174 175 176 177
    if (fd < 0) {
        return "unknown";
    }
    char buf[512];
    const ssize_t len = read(fd, buf, sizeof(buf) - 1);
    if (len <= 0) {
        return "unknown";
    }
    buf[len] = '\0';
    // Not string(buf, len) because we needs to buf to be truncated at first \0.
    // Under gdb, the first part of cmdline may include path.
178
    return butil::FilePath(std::string(buf)).BaseName().value();
gejun's avatar
gejun committed
179 180 181
}

int ComlogSink::SetupDevice(com_device_t* dev, const char* type, const char* file, bool is_wf) {
182
    butil::FilePath path(file);
gejun's avatar
gejun committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    snprintf(dev->host, sizeof(dev->host), "%s", path.DirName().value().c_str());
    if (!is_wf) {
        snprintf(dev->name, sizeof(dev->name), "%s_0", type);
        COMLOG_SETSYSLOG(*dev);

        //snprintf(dev->file, COM_MAXFILENAME, "%s", file);
        snprintf(dev->file, sizeof(dev->file), "%s", path.BaseName().value().c_str());
    } else {
        snprintf(dev->name, sizeof(dev->name), "%s_1", type);
        dev->log_mask = 0;
        COMLOG_ADDMASK(*dev, COMLOG_WARNING);
        COMLOG_ADDMASK(*dev, COMLOG_FATAL);

        //snprintf(dev->file, COM_MAXFILENAME, "%s.wf", file);
        snprintf(dev->file, sizeof(dev->file), "%s.wf", path.BaseName().value().c_str());
    }
    
    snprintf(dev->type, COM_MAXAPPENDERNAME, "%s", type);
    dev->splite_type = static_cast<int>(_options.split_type);
    dev->log_size = _options.cut_size_megabytes; // SIZECUT precision in MB
    dev->compress = 0;
    dev->cuttime = _options.cut_interval_minutes; // DATECUT time precision in min

    // set quota conf
    int index = dev->reserved_num;
    if (dev->splite_type == COMLOG_SPLIT_SIZECUT) {
        if (_options.cut_size_megabytes <= 0) {
            LOG(ERROR) << "Invalid ComlogSinkOptions.cut_size_megabytes="
                       << _options.cut_size_megabytes;
            return -1;
        }
        if (_options.quota_size < 0) {
            LOG(ERROR) << "Invalid ComlogSinkOptions.quota_size="
                       << _options.quota_size;
            return -1;
        }
        snprintf(dev->reservedext[index].name, sizeof(dev->reservedext[index].name),
                 "%s_QUOTA_SIZE", dev->name);
        snprintf(dev->reservedext[index].value, sizeof(dev->reservedext[index].value),
                 "%d", _options.quota_size);
        index++;
    } else if (dev->splite_type == COMLOG_SPLIT_DATECUT) {
        if (_options.quota_day < 0) {
            LOG(ERROR) << "Invalid ComlogSinkOptions.quota_day=" << _options.quota_day;
            return -1;
        }
        if (_options.quota_hour < 0) {
            LOG(ERROR) << "Invalid ComlogSinkOptions.quota_hour=" << _options.quota_hour;
            return -1;
        }
        if (_options.quota_min < 0) {
            LOG(ERROR) << "Invalid ComlogSinkOptions.quota_min=" << _options.quota_min;
            return -1;
        }
        if (_options.quota_day > 0) {
            snprintf(dev->reservedext[index].name, sizeof(dev->reservedext[index].name),
                     "%s_QUOTA_DAY", (char*)dev->name);
            snprintf(dev->reservedext[index].value, sizeof(dev->reservedext[index].value),
                     "%d", _options.quota_day);
            index++;
        }
        if (_options.quota_hour > 0) {
            snprintf(dev->reservedext[index].name, sizeof(dev->reservedext[index].name),
                     "%s_QUOTA_HOUR", (char*)dev->name);
            snprintf(dev->reservedext[index].value, sizeof(dev->reservedext[index].value),
                     "%d", _options.quota_hour);
            index++;
        }
        if (_options.quota_min > 0) {
            snprintf(dev->reservedext[index].name, sizeof(dev->reservedext[index].name),
                     "%s_QUOTA_MIN", (char*)dev->name);
            snprintf(dev->reservedext[index].value, sizeof(dev->reservedext[index].value),
                     "%d", _options.quota_min);
            index++;
        }
    }
    dev->reserved_num = index;
    dev->reservedconf.item = &dev->reservedext[0];
    dev->reservedconf.num = dev->reserved_num;
    dev->reservedconf.size = dev->reserved_num;

    ComlogLayoutOptions layout_options;
    layout_options.shorter_log_level = _options.shorter_log_level;
    ComlogLayout* layout = new (std::nothrow) ComlogLayout(&layout_options);
    if (layout == NULL) {
        LOG(FATAL) << "Fail to new layout";
        return -1;
    }
    dev->layout = layout;

    return 0;
}

int ComlogSink::Setup(const ComlogSinkOptions* options) {
    Unload();
    if (options) {
        _options = *options;
    }
    if (_options.max_log_length > 0) {
        comspace::Event::setMaxLogLength(_options.max_log_length);
    }
    if (_options.process_name.empty()) {
        _options.process_name = GetProcessName();
    }

    char type[COM_MAXAPPENDERNAME];
    if (_options.async) {
        snprintf(type, COM_MAXAPPENDERNAME, "AFILE");
    } else {
        snprintf(type, COM_MAXAPPENDERNAME, "FILE");
    }
294
    butil::FilePath cwd;
gejun's avatar
gejun committed
295
    if (!_options.log_dir.empty()) {
296
        butil::FilePath log_dir(_options.log_dir);
gejun's avatar
gejun committed
297 298 299
        if (log_dir.IsAbsolute()) {
            cwd = log_dir;
        } else {
300
            if (!butil::GetCurrentDirectory(&cwd)) {
gejun's avatar
gejun committed
301 302 303 304 305 306
                LOG(ERROR) << "Fail to get cwd";
                return -1;
            }
            cwd = cwd.Append(log_dir);
        }
    } else {
307
        if (!butil::GetCurrentDirectory(&cwd)) {
gejun's avatar
gejun committed
308 309 310 311
            LOG(ERROR) << "Fail to get cwd";
            return -1;
        }
    }
312 313
    butil::File::Error err;
    if (!butil::CreateDirectoryAndGetError(cwd, &err)) {
gejun's avatar
gejun committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        LOG(ERROR) << "Fail to create directory, " << err;
        return -1;
    }
    char file[COM_MAXFILENAME];
    snprintf(file, COM_MAXFILENAME, "%s",
             cwd.Append(_options.process_name + ".log").value().c_str());

    int dev_num = (_options.enable_wf_device ? 2 : 1);
    _dev = new (std::nothrow) com_device_t[dev_num];
    if (NULL == _dev) {
        LOG(FATAL) << "Fail to new com_device_t";
        return -1;
    }
    if (0 != SetupDevice(&_dev[0], type, file, false)) {
        LOG(ERROR) << "Fail to setup first com_device_t";
        return -1;
    }
    if (dev_num == 2) {
        if (0 != SetupDevice(&_dev[1], type, file, true)) {
            LOG(ERROR) << "Fail to setup second com_device_t";
            return -1;
        }
    }
    if (com_openlog(_options.process_name.c_str(), _dev, dev_num, NULL) != 0) {
        LOG(ERROR) << "Fail to com_openlog";
        return -1;
    }
    _init = true;
    return 0;
}
        
void ComlogSink::Unload() {
    if (_init) {
        com_closelog(0);
        _init = false;
    }
    if (_dev) {
        // FIXME(gejun): Can't delete layout, somewhere in comlog may still
        // reference the layout after com_closelog.
        //delete _dev->layout;
        delete [] _dev;
        _dev = NULL;
    }
}

ComlogSink::~ComlogSink() {
    Unload();
}

int const comlog_levels[LOG_NUM_SEVERITIES] = {
    COMLOG_TRACE, COMLOG_NOTICE, COMLOG_WARNING, COMLOG_FATAL, COMLOG_FATAL };

bool ComlogSink::OnLogMessage(int severity, const char* file, int line,
367
                              const butil::StringPiece& content) {
gejun's avatar
gejun committed
368 369 370 371 372 373 374 375
    // Print warning for VLOG since many online servers do not enable COMLOG_TRACE.
    int comlog_level = 0;
    if (severity < 0) {
        comlog_level = _options.print_vlog_as_warning ? COMLOG_WARNING : COMLOG_TRACE;
    } else {
        comlog_level = comlog_levels[severity];
    }
    if (FLAGS_log_hostname) {
376
        butil::StringPiece hostname(butil::my_hostname());
gejun's avatar
gejun committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390
        if (hostname.ends_with(".baidu.com")) { // make it shorter
            hostname.remove_suffix(10);
        }
        return com_writelog(comlog_level, "%.*s %s:%d] %.*s",
                            (int)hostname.size(), hostname.data(),
                            file, line,
                            (int)content.size(), content.data()) == 0;
    }
    // Using %.*s is faster than %s.
    return com_writelog(comlog_level, "%s:%d] %.*s", file, line,
                        (int)content.size(), content.data()) == 0;
}

}  // namespace logging