diff --git a/LICENSE b/LICENSE index 4042734..c06f6b3 100644 --- a/LICENSE +++ b/LICENSE @@ -58,7 +58,7 @@ APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright [2023] [Marcel Bandić] +Copyright [2023] [Marcel Bandić - marcelb96@yahoo.com] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index e69de29..dcf224d 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,61 @@ + +# A library for tracking statistics and metrics + +A simple library for measuring incremental parameters in C++ programs based on the key-value principle. + +## Features + +- Object oriented +- Thread safe +- Easy access to counters using the [] operator +- Simple incrementing of counters using the ++ operator +- Initial setup of the list of counters and values +- List of counter names +- Retrieving the status of all counters +- Reset all counters +- Retrieving the state of all counters and resetting the counters +## Installation + +Just download the latest release and unzip it into your project. You can turn it on with: + +``` +#include "metrics/lib/metrics.hpp" +using namespace marcelb; +``` + +## Usage + +```c++ +// init +Metrics stats; +// operator [] and increment ++ +stats["access"]++; +// print couter access +cout << stats["access"]; +// get counters names +auto listCounterNames = stats.keys(); +// get all couters +auto data = stats.get_data(); +// reset counters +stats.clear(); +// set counters +map MyStats = {{"access", 3},{ "error", 0}}; +stats.set(MyStats); +// get and clear all counters +auto dataWithClear = stats.get_data_and_clear(); +``` +## License + +[APACHE 2.0](http://www.apache.org/licenses/LICENSE-2.0/) + + +## Support & Feedback + +For support and any feedback, contact the address: marcelb96@yahoo.com. + +## Contributing + +Contributions are always welcome! + +Feel free to fork and start working with or without a later pull request. Or contact for suggest and request an option. + diff --git a/lib/metrics.hpp b/lib/metrics.hpp index a580079..f75130c 100644 --- a/lib/metrics.hpp +++ b/lib/metrics.hpp @@ -11,28 +11,34 @@ using namespace std; namespace marcelb { - /** - * Klasa za mjerenje proizvoljne statistike - */ - +/** + * A class for measuring arbitrary statistics +*/ class Metrics { mutex io; map counters; public: + /** + * Constructor, without predefining the name of the counter + */ Metrics (); + + /** + * Constructor, with predefined counters from the passed object + */ Metrics (map _counters); /** - * Operator[] za pristup svakoj brojaču mjerenja + * Operator[] to access each measurement counter */ uint& operator[](const string& key) { return counters[key]; } /** - * Operator++ za inkrementalno povećanje brojača mjerenja + * Operator++ to increment the measurement counter incrementally */ Metrics& operator++ (int n) { lock_guard _io(io); @@ -40,27 +46,30 @@ class Metrics { return *this; } + /** + * Method to set the counter from the passed object + */ void set(map _counters) { counters = _counters; } /** - * Metoda za resetiranje brojača + * A method to reset the counter */ void clear(); /** - * Metoda koja vraća vektor stringova svih naziva brojača + * A method that returns a vector os strings of all counter names */ vector keys(); /** - * Metoda vraća map svih mjerenja + * The method returns a map of all measurements */ map get_data(); /** - * Metoda vraća map svih mjerenja i resetira brojače + * The method returns a map of all measurements and resets the counters */ map get_data_and_clear(); diff --git a/test/test b/test/test index 3733600..db2161a 100755 Binary files a/test/test and b/test/test differ diff --git a/test/test.cpp b/test/test.cpp index cc0fa24..19ce70b 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -16,7 +16,23 @@ int main() { cout << " error " << metrika["error"] << endl; cout << " access " << metrika["access"] << endl; - + // // init + // Metrics stats; + // // operator [] and increment ++ + // stats["access"]++; + // // print couter access + // cout << stats["access"]; + // // get counters names + // auto listCounterNames = stats.keys(); + // // get all couters + // auto data = stats.get_data(); + // // reset counters + // stats.clear(); + // // set counters + // map MyStats = {{"access", 3},{ "error", 0}}; + // stats.set(MyStats); + // // get and clear all counters + // auto dataWithClear = stats.get_data_and_clear(); return 0; -} \ No newline at end of file +}