GithubHelp home page GithubHelp logo

junhaoliao / clp-ffi-go Goto Github PK

View Code? Open in Web Editor NEW

This project forked from y-scope/clp-ffi-go

0.0 0.0 0.0 484 KB

clp-ffi-go is a library to encode log messages with CLP, and work with the encoded messages using a foreign function interface (FFI).

License: Apache License 2.0

C++ 24.77% C 30.09% Go 40.43% CMake 3.28% Starlark 1.43%

clp-ffi-go's Introduction

clp-ffi-go

CLP on Zulip

This module provides Go packages to interface with CLP's core features through CLP's FFI (foreign function interface). For complete technical documentation see the Go docs: https://pkg.go.dev/github.com/y-scope/clp-ffi-go

Getting started

To add the module to your project run: go get github.com/y-scope/clp-ffi-go

Here's an example showing how to decode each log event containing "ERROR" from a CLP IR byte stream.

import (
  "fmt"
  "time"

  "github.com/klauspost/compress/zstd"
  "github.com/y-scope/clp-ffi-go/ffi"
  "github.com/y-scope/clp-ffi-go/ir"
)

file, _ := os.Open("log-file.clp.zst")
defer file.Close()
zstdReader, _ := zstd.NewReader(file)
defer zstdReader.Close()
irReader, _ := ir.NewReader(zstdReader)
defer irReader.Close()

var err error
for {
  var log *ffi.LogEventView
  // To read every log event replace ReadToContains with Read()
  log, err = irReader.ReadToContains("ERROR")
  if nil != err {
    break
  }
  fmt.Printf("%v %v", time.UnixMilli(int64(log.Timestamp)), log.LogMessageView)
}
if ir.EndOfIr != err {
  fmt.Printf("Reader.Read failed: %v", err)
}

Building

We use the go generate command to build the C++ interface to CLP's FFI code as well as stringify Enum style types.

  1. Install requirements:
    1. A C++ compiler that supports C++17
    2. CMake 3.11 or higher
    3. The Stringer tool: https://pkg.go.dev/golang.org/x/tools/cmd/stringer
      • go install golang.org/x/tools/cmd/stringer@latest
  2. go generate ./...
    • Run all generate directives (note the 3 dots after '/')

Bazel support

We provide Bazel build files for each Go package in the repository, enabling you to add any package to your build dependency list with no extra arguments or modifications.

Why not build with cgo?

The primary reason we choose to build with CMake rather than directly with cgo, is to ease code maintenance by maximizing the reuse of CLP's code with no modifications. If a platform you use is not supported by the pre-built libraries, please open an issue and we can integrate it into our build process.

Testing

To run all unit tests run: go_test_ir="/path/to/my-ir.clp.zst" go test ./...

  • Some of the ir package's tests currently require an existing CLP IR file compressed with zstd. This file's path is taken as an environment variable named go_test_ir. It can be an absolute path or a path relative to the ir directory.

Linting

  1. Install golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | \
  sh -s -- -b $(go env GOPATH)/bin v1.59.0
  1. Run with golangci-lint run

Using an external C++ library

Use the external build tag to link with different CLP FFI library instead of the pre-built ones found in lib. This tag only prevents the linking of the pre-built libraries and does nothing else. It is up to the user to use CGO_LDFLAGS to point to their library. You may also need to update CGO_CFLAGS to update the header include path.

For example, to run the tests using the external you can run:

CGO_LDFLAGS="-L/path/to/external_libs -lclp_ffi_linux_amd64 -Wl,-rpath=/path/to/external_libs" \
  go_test_ir="/path/to/my-ir.clp.zst" \
  go test -tags external ./...

clp-ffi-go's People

Contributors

davidlion avatar junhaoliao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.