Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TUBEMQ-268]Tubemq client cpp log module #188

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
path = tubemq-client-twins/tubemq-client-cpp/third_party/rapidjson
url = https://github.com/Tencent/rapidjson.git
tag = v1.1.0
[submodule "tubemq-client-twins/tubemq-client-cpp/third_party/log4cplus"]
path = tubemq-client-twins/tubemq-client-cpp/third_party/log4cplus
url = https://github.com/log4cplus/log4cplus.git
tag = REL_2_0_5
21 changes: 21 additions & 0 deletions tubemq-client-twins/tubemq-client-cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#TubeMQ Client CPP#

22 changes: 22 additions & 0 deletions tubemq-client-twins/tubemq-client-cpp/example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#Example#

tubemq-client-cpp example.
28 changes: 28 additions & 0 deletions tubemq-client-twins/tubemq-client-cpp/example/log/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need license

#include <chrono>
#include <exception>
#include <iostream>
#include <string>
#include <thread>

#include "logger.h"

using namespace std;
using namespace tubemq;

void log() {
int i = 0;
while (1) {
LOG_ERROR("threadid:%ld, i:%d", std::this_thread::get_id(), i++);
}
}

int main() {
std::thread t1(log);
std::thread t2(log);
std::thread t3(log);
std::thread t4(log);
t1.join();
return 0;
}

109 changes: 109 additions & 0 deletions tubemq-client-twins/tubemq-client-cpp/inc/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#ifndef _TUBEMQ_LOG_FILE_
#define _TUBEMQ_LOG_FILE_

#include <string>
#include <vector>

namespace tubemq {
class Logger;

Logger& GetLogger();

#define LOG_LEVEL(level, fmt, ...) \
{ \
if (tubemq::GetLogger().IsEnable(level)) { \
tubemq::GetLogger().Write("[%s:%d][%s]" fmt, __func__, __LINE__, tubemq::Logger::Level2String(level), \
##__VA_ARGS__); \
} \
}

#define LOG_TRACE(fmt, ...) LOG_TUBE(tubemq::GetLogger(), tubemq::Logger::kTrace, fmt, ##__VA_ARGS__)
#define LOG_DEBUG(fmt, ...) LOG_TUBE(tubemq::GetLogger(), tubemq::Logger::kDebug, fmt, ##__VA_ARGS__)
#define LOG_INFO(fmt, ...) LOG_TUBE(tubemq::GetLogger(), tubemq::Logger::kInfo, fmt, ##__VA_ARGS__)
#define LOG_WARN(fmt, ...) LOG_TUBE(tubemq::GetLogger(), tubemq::Logger::kWarn, fmt, ##__VA_ARGS__)
#define LOG_ERROR(fmt, ...) LOG_TUBE(tubemq::GetLogger(), tubemq::Logger::kError, fmt, ##__VA_ARGS__)

#define LOG_TUBE(logger, level, fmt, ...) \
{ \
if (logger.IsEnable(level)) { \
logger.Write("[%s:%d][%s]" fmt, __func__, __LINE__, tubemq::Logger::Level2String(level), ##__VA_ARGS__); \
} \
}

class Logger {
public:
enum Level
{
kTrace = 0,
kDebug = 1,
kInfo = 2,
kWarn = 3,
kError = 4,
};

// size: MB
Logger() : file_max_size_(100), file_num_(10), level_(kError), base_path_("tubemq"), instance_("TubeMQ") { setup(); }

~Logger(void) {}

// path example: ../log/tubemq
// size: MB
bool Init(const std::string& path, Level level, uint32_t file_max_size = 100, uint32_t file_num = 10);

bool Write(const char* sFormat, ...) __attribute__((format(printf, 2, 3)));
inline bool WriteStream(const std::string& msg) { return writeStream(msg.c_str()); }

inline void SetInstance(const std::string& instance) { instance_ = instance; }
inline bool IsEnable(Level level) {
if (level_ <= level) {
return true;
} else {
return false;
}
}

static const char* Level2String(Level level) {
static const char* level_names[] = {
"TRACE",
"DEBUG"
"INFO",
"WARN",
"ERROR",
};
return level_names[level];
}

private:
void setup();
bool writeStream(const char* msg);

private:
uint32_t file_max_size_;
uint16_t file_num_;
uint8_t level_;

std::string base_path_;
std::string instance_;
std::string err_msg_;
};
} // namespace tubemq
#endif // _TUBEMQ_LOG_FILE_
37 changes: 37 additions & 0 deletions tubemq-client-twins/tubemq-client-cpp/inc/noncopyable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#ifndef _TUBUMQ_NONCOPYABLE_H
#define _TUBUMQ_NONCOPYABLE_H

namespace tubemq {

class noncopyable {
public:
noncopyable(const noncopyable&) = delete;
void operator=(const noncopyable&) = delete;

protected:
noncopyable() = default;
~noncopyable() = default;
};

} // namespace tubemq

#endif // _TUBUMQ_NONCOPYABLE_H
60 changes: 60 additions & 0 deletions tubemq-client-twins/tubemq-client-cpp/inc/singleton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#ifndef _TUBEMQ_SINGLETON_H
#define _TUBEMQ_SINGLETON_H

#include <thread>
#include <mutex>
#include <assert.h>
#include <stdlib.h>

#include "noncopyable.h"

namespace tubemq {

template <typename T>
class Singleton : noncopyable {
public:
Singleton() = delete;
~Singleton() = delete;

static T& instance() {
std::call_once(once_, Singleton::init);
assert(value_ != nullptr);
return *value_;
}

private:
static void init() { value_ = new T(); }

private:
static std::once_flag once_;
static T* value_;
};

template <typename T>
std::once_flag Singleton<T>::once_;

template <typename T>
T* Singleton<T>::value_ = nullptr;

} // namespace tubemq

#endif // _TUBEMQ_SINGLETON_H
40 changes: 40 additions & 0 deletions tubemq-client-twins/tubemq-client-cpp/inc/unique_seq_id.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#ifndef TUBEMQ_UNIQUESEQID_H
#define TUBEMQ_UNIQUESEQID_H

#include <atomic>
#include <stdint.h>

namespace tubemq {

class UniqueSeqId {
public:
UniqueSeqId() : id(0) {}

uint32_t Next() { return id.fetch_add(1, std::memory_order_relaxed); }

protected:
std::atomic<uint32_t> id;
};

} // namespace tubemq

#endif
Empty file.
Loading