-
Notifications
You must be signed in to change notification settings - Fork 505
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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# | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
#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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need license