Add proper logging

This commit is contained in:
2024-10-17 20:02:54 -05:00
parent 66897bca0c
commit d2e062d03d
6 changed files with 92 additions and 31 deletions

View File

@@ -22,8 +22,8 @@
using namespace std::chrono_literals;
const int max_clients = config->getInt("net", "max_clients", 255);
const int cport = config->getInt("net", "control_port", 21);
const uint16_t max_clients = config->getInt("net", "max_clients", 255);
const uint16_t cport = config->getInt("net", "control_port", 21);
struct pollfd fds[65535];
struct clientfd {
@@ -41,10 +41,9 @@ void runClient(struct clientfd* cfd) {
//while (fcntl(cfd->client->control_sock, F_GETFD) != -1) {
while (true) {
if (cfd->client == nullptr) { break; }
printf("[d] C(%i) Attempting read...\n", cfd->client->control_sock);
int rc = recv(cfd->client->control_sock, inbuf, sizeof(inbuf), 0);
if (rc < 0) {
printf("[d] C(%i) Recieved empty packet\n", cfd->client->control_sock);
logger->print(LOGLEVEL_WARNING, "C(%i) Recieved empty packet", cfd->client->control_sock);
if (errno != EWOULDBLOCK) {
perror("recv() failed");
break;
@@ -65,19 +64,20 @@ void runClient(struct clientfd* cfd) {
std::string args = "";
if (len > cmdend) args = lin.substr(cmdend+1, len-cmdend-1);
printf("[d] C(%i) >> '%s' '%s'\n", cfd->client->control_sock, cmd.c_str(), args.c_str());
logger->print(LOGLEVEL_DEBUG, "C(%i) >> '%s' '%s'", cfd->client->control_sock, cmd.c_str(), args.c_str());
if (cfd->client->receive(cmd, args) < 0) break;
inbuf[0] = '\0';
}
} catch (...) {
printf("[!] C(%i) Caught error!\n", cfd->client->control_sock);
logger->print(LOGLEVEL_ERROR, "C(%i) Caught error!", cfd->client->control_sock);
}
//printf("[d] C(%i) Marking for deletion...\n", cfd->client->control_sock);
cfd->close = true;
}
int main(int argc , char *argv[]) {
logger = new Logger(config->getValue("logging", "file", "digftp.log").c_str());
logger->setLevel(config->getInt("logging", "level", 0));
std::string authType = config->getValue("main", "auth_engine", "plain");
auth = getAuthByName(authType);
auth->setOptions(config->get(authType));
@@ -129,7 +129,7 @@ int main(int argc , char *argv[]) {
fds[0].fd = master_socket;
fds[0].events = POLLIN;
printf("[i] Server started.\n");
logger->print(LOGLEVEL_INFO, "Server started.");
while (run) {
int pc = poll(fds, nfds, -1);
@@ -139,7 +139,7 @@ int main(int argc , char *argv[]) {
}
if (pc == 0) {
printf("poll() timed out\n");
perror("poll() timed out\n");
break;
}
@@ -149,7 +149,7 @@ int main(int argc , char *argv[]) {
continue;
if(fds[i].revents != POLLIN) {
printf("[!] C(%i) Error! revents = %d\n", fds[i].fd, fds[i].revents);
logger->print(LOGLEVEL_ERROR, "C(%i) Error! revents = %d", fds[i].fd, fds[i].revents);
goto conn_close;
}
if (fds[i].fd == master_socket) {
@@ -162,8 +162,7 @@ int main(int argc , char *argv[]) {
}
break;
}
printf("[d] C(%i) Accepted client\n", newsock);
logger->print(LOGLEVEL_DEBUG, "C(%i) Accepted client", newsock);
fds[nfds].fd = newsock;
fds[nfds].events = POLLIN;
fdc[nfds].close = false;
@@ -174,7 +173,7 @@ int main(int argc , char *argv[]) {
} else {
if (fdc[i].close) {
conn_close:
printf("[d] C(%i) Deleting client...\n", fds[i].fd);
logger->print(LOGLEVEL_DEBUG, "C(%i) Deleting client...", fds[i].fd);
close(fds[i].fd);
fds[i].fd = -1;
if (fdc[i].client->thread.joinable())
@@ -193,15 +192,15 @@ int main(int argc , char *argv[]) {
if (fds[i].fd == -1) {
for(int j = i; j < nfds; j++) {
if (fds[j].fd == -1) {
printf("[d] Compressing: %i(fd:%i) <= %i(fd:%i)\n", j, fds[j].fd, j+1, fds[j+1].fd);
logger->print(LOGLEVEL_DEBUG, "Compressing: %i(fd:%i) <= %i(fd:%i)", j, fds[j].fd, j+1, fds[j+1].fd);
fds[j].fd = fds[j+1].fd;
fds[j].revents = fds[j+1].revents;
fds[j+1].fd = -1;
printf("[d] Reinitialized fds of %i\n", j+1);
logger->print(LOGLEVEL_DEBUG, "Reinitialized fds of %i", j+1);
fdc[j].client = fdc[j+1].client;
fdc[j].close = fdc[j+1].close;
fdc[j+1] = {};
printf("[d] Reinitialized fdc of %i\n", j+1);
logger->print(LOGLEVEL_DEBUG, "[d] Reinitialized fdc of %i", j+1);
}
}
i--;
@@ -210,5 +209,6 @@ int main(int argc , char *argv[]) {
}
}
}
logger->close();
return 0;
}