Skip to content

Commit

Permalink
Initial work with flat & zmq
Browse files Browse the repository at this point in the history
  • Loading branch information
0gap committed Dec 12, 2021
1 parent cfc7628 commit 8fa9ca3
Show file tree
Hide file tree
Showing 12 changed files with 496 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Generated from CLion C/C++ Code Style settings
BasedOnStyle: Google
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: Align
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Allman
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: true
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 80
Cpp11BracedListStyle: false
CompactNamespaces: false
ContinuationIndentWidth: 8
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Right
ReflowComments: false
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 0
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 2
UseTab: Never
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.idea/

cmake-build-*/
49 changes: 49 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
cmake_minimum_required(VERSION 3.20)
project(msg_logging)

set(CMAKE_CXX_STANDARD 17)

set(generated_dir ${CMAKE_BINARY_DIR}/generated)
file(MAKE_DIRECTORY ${generated_dir})
include_directories(${generated_dir} ${CMAKE_SOURCE_DIR})
message("** generated_dir............: ${generated_dir}")
message("** source dir............: ${CMAKE_SOURCE_DIR}")
add_custom_command(
OUTPUT ${generated_dir}/log_msg_generated.h
COMMAND flatc --cpp --scoped-enums --grpc ${CMAKE_SOURCE_DIR}/log_msg.fbs
WORKING_DIRECTORY ${generated_dir}
DEPENDS ${CMAKE_SOURCE_DIR}/log_msg.fbs
)
set_source_files_properties(${generated_dir}/log_msg_buf.h
PROPERTIES GENERATED TRUE)

if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION true)
set(CMAKE_CXX_FLAGS "-g3 -Wall -fuse-linker-plugin -O3 -DNDEBUG")
endif ()

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

# compile the flatbuffer file
add_custom_target(flat_compiled_headers ALL
DEPENDS ${generated_dir}/log_msg_generated.h
)

add_library(flat_msg OBJECT lib/LogEntrySerializer.cc lib/LogEntrySerializer.h
${generated_dir}/log_msg_generated.h
${generated_dir}/log_msg.grpc.fb.cc
)
add_dependencies(flat_msg flat_compiled_headers)

add_executable(msg_logging main.cpp)
add_dependencies(msg_logging flat_msg)
target_include_directories(msg_logging BEFORE PRIVATE lib)
target_link_libraries(msg_logging ${CONAN_LIBS} flat_msg)

add_executable(zmq_server
zmq_consumer/zmq_log_server.cc
zmq_consumer/zmq_consumer.cc)
target_link_libraries(zmq_server ${CONAN_LIBS} flat_msg)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/)
12 changes: 12 additions & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[requires]
grpc/1.39.1
gtest/cci.20210126
cppzmq/4.8.1
flatbuffers/2.0.0
glog/0.5.0
benchmark/1.6.0
gflags/2.2.2
abseil/20210324.2

[generators]
cmake
41 changes: 41 additions & 0 deletions lib/LogEntrySerializer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Created by zerogap on 12/12/21.
//

#include "LogEntrySerializer.h"

LogEntrySerializer::LogEntrySerializer(Logger::LogLevel lvl,
std::string code_point,
std::string log_msg,
std::string srvc_name,
std::string host):builder(1024) {

fb_lvl = lvl;
fb_code_point = code_point;//builder.CreateString(code_point);
fb_log_msg = log_msg; //builder.CreateString(log_msg);
fb_srvc_name = srvc_name; //builder.CreateString(srvc_name);
fb_host = host; //builder.CreateString(host);
}

void LogEntrySerializer::export_fb(){
auto code_point = builder.CreateString(fb_code_point);
auto log_msg = builder.CreateString(fb_log_msg);
auto srvc_name = builder.CreateString(fb_srvc_name);
auto host = builder.CreateString(fb_host);

Logger::LogEntryBuilder builder_(builder);
builder_.add_lvl(fb_lvl);
builder_.add_log_msg(log_msg);
builder_.add_code_point(code_point);
builder_.add_srvc_name(srvc_name);
builder_.add_host(host);
auto pce = builder_.Finish();
builder.Finish(pce);
buf = builder.GetBufferPointer();
size = builder.GetSize();
}

std::string_view LogEntrySerializer::get_data() {
return std::string_view{reinterpret_cast<char*>(builder.GetBufferPointer()),
builder.GetSize()};
}
33 changes: 33 additions & 0 deletions lib/LogEntrySerializer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Created by zerogap on 12/12/21.
//

#ifndef MSG_LOGGING_LOGENTRYSERIALIZER_H
#define MSG_LOGGING_LOGENTRYSERIALIZER_H

#include "log_msg_generated.h"
#include "flatbuffers/flatbuffers.h"

struct LogEntrySerializer {
Logger::LogLevel fb_lvl;
std::string fb_code_point;
std::string fb_log_msg;
std::string fb_srvc_name;
std::string fb_host;
uint8_t *buf {nullptr};
size_t size{ 0}; // Returns the size of the buffer that
flatbuffers::FlatBufferBuilder builder;

explicit LogEntrySerializer(Logger::LogLevel lvl,
std::string code_point,
std::string log_msg,
std::string srvc_name,
std::string host);

void export_fb();

std::string_view get_data();
};


#endif//MSG_LOGGING_LOGENTRYSERIALIZER_H
31 changes: 31 additions & 0 deletions log_msg.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// example IDL file

namespace Logger;

file_identifier "LMSG";

enum LogLevel : byte { INFO = 0, DEBUG = 5, TRACE, INSANE }

table LogId {
id:long = 0;
}

table LogEntry {
lvl:LogLevel = INFO;
code_point:string;
log_msg:string;
srvc_name:string;
host:string;
}

table LogSendResponse {
success:bool = false;
msg:string;
}

rpc_service LogComs {
Send(LogEntry):LogSendResponse;
Retrieve(LogId):LogEntry;
}

root_type LogEntry;
28 changes: 28 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "LogEntrySerializer.h"
#include <glog/logging.h>
#include <iostream>
#include <zmq_addon.hpp>

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#define __CODE_POINT__ (__FILE__ "::" STR(__LINE__))

int main() {

for (int j = 0; j < 5; ++j) {

zmq::context_t context(1);
zmq::socket_t brokerA(context, zmq::socket_type::push);
brokerA.connect("tcp://localhost:52236");
LOG(INFO) << "Connected to socket";
for (int i = 0; i < 5; ++i) {

LogEntrySerializer log_to_send(Logger::LogLevel::INSANE, __CODE_POINT__, "hello from msg " + std::to_string(i), "this service", "hostname");
log_to_send.export_fb();
brokerA.send(zmq::const_buffer(zmq::buffer(log_to_send.get_data())), zmq::send_flags::dontwait);
}
brokerA.close();

}
return 0;
}
21 changes: 21 additions & 0 deletions zmq_consumer/message_processor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Created by zerogap on 12/12/21.
//

#ifndef MSG_LOGGING_MESSAGE_PROCESSOR_H
#define MSG_LOGGING_MESSAGE_PROCESSOR_H

#include <glog/logging.h>
#include <memory>
#include <vector>

#include <zmq.hpp>

namespace msg_logging::zmq_logger {
class MessageHandler {
public:
virtual ~MessageHandler(){};
virtual bool handle_message(zmq::message_t &header, zmq::message_t &body) { return false; };
};
}// namespace msg_logging::zmq_logger
#endif// MSG_LOGGING_MESSAGE_PROCESSOR_H
Loading

0 comments on commit 8fa9ca3

Please sign in to comment.