Skip to content

Instantly share code, notes, and snippets.

@judeapana
Created April 25, 2023 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judeapana/901a5b4a9ebb625c8de06ddd84dd2f71 to your computer and use it in GitHub Desktop.
Save judeapana/901a5b4a9ebb625c8de06ddd84dd2f71 to your computer and use it in GitHub Desktop.
This is a logging system built using blockchain and programming language Solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract SecureLogging {
address private owner;
mapping (uint => Log) private logs;
uint private logCount;
struct Log {
uint timestamp;
string message;
string tag;
string identity;
}
constructor() {
owner = msg.sender;
logCount = 0;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only the contract owner can call this function.");
_;
}
function addLog(string memory _message,string memory _tag,string memory _identity) public onlyOwner returns (uint) {
logCount++;
logs[logCount] = Log(block.timestamp, _message,_tag,_identity);
return logCount;
}
function getLog(uint _logId) public view returns (uint, string memory,string memory,string memory) {
require(_logId > 0 && _logId <= logCount, "Invalid log ID.");
return (logs[_logId].timestamp, logs[_logId].message,logs[_logId].tag,logs[_logId].identity);
}
function getLogs() public view returns (Log[] memory) {
Log[] memory allLogs = new Log[](logCount);
for (uint i = 0; i < logCount; i++) {
allLogs[i] = logs[i];
}
return allLogs;
}
function searchLogs(uint timestamp, string memory message, string memory tag, string memory identity) public view returns (Log[] memory) {
Log[] memory matchingLogs = new Log[](logCount);
uint matchingLogsIndex = 0;
for (uint i = 0; i < logCount; i++) {
if (logs[i].timestamp == timestamp || bytes(message).length == 0 || keccak256(bytes(logs[i].message)) == keccak256(bytes(message)) || bytes(tag).length == 0 || keccak256(bytes(logs[i].tag)) == keccak256(bytes(tag)) || keccak256(bytes(logs[i].identity)) == keccak256(bytes(identity))) {
matchingLogs[matchingLogsIndex] = logs[i];
matchingLogsIndex++;
}
}
Log[] memory result = new Log[](matchingLogsIndex);
for (uint i = 0; i < matchingLogsIndex; i++) {
result[i] = matchingLogs[i];
}
return result;
}
function getLatestLog() public view returns (uint, string memory,string memory,string memory) {
require(logCount > 0, "No logs found.");
return (logs[logCount].timestamp, logs[logCount].message,logs[logCount].tag,logs[logCount].identity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment