🌿 Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms"

This is a C++ Notification System designed to be integrated into any C++ program. It provides real-time notifications via console messages, log files, or emails. This system can be useful for applications like banking, rentals, and other C++ projects that require alert mechanisms.
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
using namespace std;
// Types of notifications
enum NotificationType { CONSOLE, LOGFILE };
// Function for getting current timestamp
string getTimestamp() {
time_t now = time(0);
char buffer[80];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime(&now));
return string(buffer);
}
// Notification System Class
class NotificationSystem {
public:
static void sendNotification(const string& message, NotificationType type = CONSOLE) {
string timestampedMessage = "[" + getTimestamp() + "] " + message;
switch (type) {
case CONSOLE:
cout << timestampedMessage << endl;
break;
case LOGFILE:
logToFile(timestampedMessage);
break;
}
}
private:
static void logToFile(const string& message) {
ofstream logFile("notifications.log", ios::app);
if (logFile.is_open()) {
logFile << message << endl;
logFile.close();
} else {
cerr << "Error: Unable to open log file!" << endl;
}
}
};
// Example usage (can be called from another program)
int main() {
NotificationSystem::sendNotification("New user registered.", CONSOLE);
NotificationSystem::sendNotification("Transaction failed. Logging error...", LOGFILE);
return 0;
}
To use this notification system in an existing C++ program, follow these steps:
Add the following line to the existing program to include the Notification System:
#include "NotificationSystem.h"
sendNotification
FunctionInvoke the function in the program wherever notifications are required. Example:
NotificationSystem::sendNotification("New transaction detected: $500 withdrawn.", LOGFILE);
This C++ Notification System allows seamless integration with other programs, providing essential notifications through multiple channels. It is lightweight, modular, and can be extended to support additional features like email alerts.
Comments
Post a Comment