mk_util.cpp 1.64 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
5
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
9 10
 */

11 12
#include "mk_util.h"
#include <stdarg.h>
xiongziliang committed
13
#include <assert.h>
14 15 16 17
#include "Util/logger.h"
using namespace std;
using namespace toolkit;

18 19 20 21
#ifndef _WIN32
#define _strdup strdup
#endif

22
API_EXPORT char* API_CALL mk_util_get_exe_path(){
23
    return _strdup(exePath().data());
24 25
}

xiongziliang committed
26
API_EXPORT char* API_CALL mk_util_get_exe_dir(const char *relative_path){
27
    if(relative_path){
28
        return _strdup((exeDir() + relative_path).data());
29
    }
30
    return _strdup(exeDir().data());
31 32
}

xiongziliang committed
33
API_EXPORT uint64_t API_CALL mk_util_get_current_millisecond(){
34 35 36
    return getCurrentMillisecond();
}

xiongziliang committed
37
API_EXPORT char* API_CALL mk_util_get_current_time_string(const char *fmt){
38
    assert(fmt);
39
    return _strdup(getTimeStr(fmt).data());
40 41
}

xiongziliang committed
42
API_EXPORT char* API_CALL mk_util_hex_dump(const void *buf, int len){
43
    assert(buf && len > 0);
44
    return _strdup(hexdump(buf,len).data());
45 46 47 48 49 50 51 52
}

API_EXPORT void API_CALL mk_log_printf(int level, const char *file, const char *function, int line, const char *fmt, ...) {
    assert(file && function && fmt);
    LogContextCapturer info(Logger::Instance(), (LogLevel) level, file, function, line);
    va_list pArg;
    va_start(pArg, fmt);
    char buf[4096];
53
    auto n = vsnprintf(buf, sizeof(buf), fmt, pArg);
54 55 56 57 58
    buf[n] = '\0';
    va_end(pArg);
    info << buf;
}