请用C++ 写一个日志系统
时间: 2023-04-02 08:05:12 浏览: 78
好的。这是一个使用 C 语言写的简单日志系统的示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LOG_FILE "log.txt" // 日志文件名
// 记录日志
void log_message(const char* message)
{
// 获取当前时间
time_t now = time(NULL);
struct tm* tm_now = localtime(&now);
char time_str[64];
strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", tm_now);
// 写入日志文件
FILE* log_file = fopen(LOG_FILE, "a");
fprintf(log_file, "[%s] %s\n", time_str, message);
fclose(log_file);
}
int main(int argc, char** argv)
{
log_message("程序启动");
// 其他代码...
log_message("程序结束");
return 0;
}
```
该示例程序在执行时会在名为 "log.txt" 的文件中记录日志。日志的每一行都包含一个当前时间戳和一条消息。
相关问题
用c++写一个复杂的登录系统
一个复杂的登录系统可能包括以下功能:
1. 注册账户
2. 登录账户
3. 忘记密码
4. 修改密码
5. 账户锁定
6. 安全日志记录
下面是一个基于C++的简单实现:
```c++
#include <iostream>
#include <string>
#include <map>
using namespace std;
class User {
private:
string username;
string password;
int failedLoginAttempts; // 失败的登录尝试次数
bool locked; // 帐户是否被锁定
public:
User(string username, string password) {
this->username = username;
this->password = password;
this->failedLoginAttempts = 0;
this->locked = false;
}
string getUsername() {
return username;
}
bool checkPassword(string password) {
if (this->locked) {
cout << "账户已被锁定" << endl;
return false;
}
if (this->password == password) {
this->failedLoginAttempts = 0;
return true;
} else {
this->failedLoginAttempts++;
if (this->failedLoginAttempts >= 3) {
this->locked = true;
cout << "输入错误次数已达到上限,账户已被锁定" << endl;
}
return false;
}
}
bool isLocked() {
return this->locked;
}
};
class LoginSystem {
private:
map<string, User*> users; // 用户名到用户对象的映射
public:
void registerUser(string username, string password) {
if (users.count(username) > 0) {
cout << "该用户名已被注册" << endl;
} else {
User* user = new User(username, password);
users[username] = user;
cout << "注册成功" << endl;
}
}
void login(string username, string password) {
if (users.count(username) == 0) {
cout << "该用户名不存在" << endl;
} else {
User* user = users[username];
if (user->checkPassword(password)) {
cout << "登录成功" << endl;
} else {
cout << "登录失败" << endl;
}
}
}
void forgetPassword(string username) {
if (users.count(username) == 0) {
cout << "该用户名不存在" << endl;
} else {
User* user = users[username];
if (user->isLocked()) {
cout << "账户已被锁定,请联系管理员解锁" << endl;
} else {
cout << "重置密码链接已发送到注册邮箱" << endl;
}
}
}
void changePassword(string username, string oldPassword, string newPassword) {
if (users.count(username) == 0) {
cout << "该用户名不存在" << endl;
} else {
User* user = users[username];
if (user->checkPassword(oldPassword)) {
user->checkPassword(newPassword);
cout << "密码修改成功" << endl;
} else {
cout << "密码修改失败" << endl;
}
}
}
};
int main() {
LoginSystem loginSystem;
loginSystem.registerUser("user1", "password1");
loginSystem.registerUser("user2", "password2");
loginSystem.login("user1", "password1");
loginSystem.login("user1", "wrongpassword");
loginSystem.login("user1", "wrongpassword");
loginSystem.login("user1", "wrongpassword");
loginSystem.forgetPassword("user1");
loginSystem.changePassword("user1", "wrongpassword", "newpassword");
loginSystem.changePassword("user1", "password1", "newpassword");
loginSystem.login("user1", "newpassword");
return 0;
}
```
该程序中,`User`类表示一个用户,包含用户名、密码、失败的登录尝试次数和帐户是否被锁定等属性。`LoginSystem`类表示登录系统,包括注册账户、登录账户、忘记密码、修改密码等功能,使用`map`将用户名映射到用户对象,实现用户的查找和管理。在`checkPassword()`方法中,如果输入错误次数达到上限,则将帐户锁定。在其他方法中,如果帐户已被锁定,则无法进行操作。
用C++写一个复杂的登录系统
一个复杂的登录系统可能包括以下功能:
1. 注册账户
2. 登录账户
3. 忘记密码
4. 修改密码
5. 账户锁定
6. 安全日志记录
下面是一个基于C++的简单实现:
```c++
#include <iostream>
#include <string>
#include <map>
using namespace std;
class User {
private:
string username;
string password;
int failedLoginAttempts; // 失败的登录尝试次数
bool locked; // 帐户是否被锁定
public:
User(string username, string password) {
this->username = username;
this->password = password;
this->failedLoginAttempts = 0;
this->locked = false;
}
string getUsername() {
return username;
}
bool checkPassword(string password) {
if (this->locked) {
cout << "账户已被锁定" << endl;
return false;
}
if (this->password == password) {
this->failedLoginAttempts = 0;
return true;
} else {
this->failedLoginAttempts++;
if (this->failedLoginAttempts >= 3) {
this->locked = true;
cout << "输入错误次数已达到上限,账户已被锁定" << endl;
}
return false;
}
}
bool isLocked() {
return this->locked;
}
};
class LoginSystem {
private:
map<string, User*> users; // 用户名到用户对象的映射
public:
void registerUser(string username, string password) {
if (users.count(username) > 0) {
cout << "该用户名已被注册" << endl;
} else {
User* user = new User(username, password);
users[username] = user;
cout << "注册成功" << endl;
}
}
void login(string username, string password) {
if (users.count(username) == 0) {
cout << "该用户名不存在" << endl;
} else {
User* user = users[username];
if (user->checkPassword(password)) {
cout << "登录成功" << endl;
} else {
cout << "登录失败" << endl;
}
}
}
void forgetPassword(string username) {
if (users.count(username) == 0) {
cout << "该用户名不存在" << endl;
} else {
User* user = users[username];
if (user->isLocked()) {
cout << "账户已被锁定,请联系管理员解锁" << endl;
} else {
cout << "重置密码链接已发送到注册邮箱" << endl;
}
}
}
void changePassword(string username, string oldPassword, string newPassword) {
if (users.count(username) == 0) {
cout << "该用户名不存在" << endl;
} else {
User* user = users[username];
if (user->checkPassword(oldPassword)) {
user->checkPassword(newPassword);
cout << "密码修改成功" << endl;
} else {
cout << "密码修改失败" << endl;
}
}
}
};
int main() {
LoginSystem loginSystem;
loginSystem.registerUser("user1", "password1");
loginSystem.registerUser("user2", "password2");
loginSystem.login("user1", "password1");
loginSystem.login("user1", "wrongpassword");
loginSystem.login("user1", "wrongpassword");
loginSystem.login("user1", "wrongpassword");
loginSystem.forgetPassword("user1");
loginSystem.changePassword("user1", "wrongpassword", "newpassword");
loginSystem.changePassword("user1", "password1", "newpassword");
loginSystem.login("user1", "newpassword");
return 0;
}
```
该程序中,`User`类表示一个用户,包含用户名、密码、失败的登录尝试次数和帐户是否被锁定等属性。`LoginSystem`类表示登录系统,包括注册账户、登录账户、忘记密码、修改密码等功能,使用`map`将用户名映射到用户对象,实现用户的查找和管理。在`checkPassword()`方法中,如果输入错误次数达到上限,则将帐户锁定。在其他方法中,如果帐户已被锁定,则无法进行操作。