Skip to content

Commit

Permalink
Switch to using getline(3) instead of fgets(3).
Browse files Browse the repository at this point in the history
This allows reading lines of arbitrary length, and performance testing
using hyperfine didn't show any significant regressions.
  • Loading branch information
fcambus committed Nov 16, 2021
1 parent 18b169d commit 7401e74
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* https://www.logswan.org
*
* Created: 2015-05-31
* Last Updated: 2021-02-15
* Last Updated: 2021-11-16
*
* Logswan is released under the BSD 2-Clause license.
* See LICENSE file for details.
Expand All @@ -19,7 +19,6 @@

enum {
HLL_BITS = 20,
LINE_LENGTH_MAX = 65536,
STATUS_CODE_MAX = 512,

CONTINENTS = 7,
Expand Down
8 changes: 5 additions & 3 deletions src/logswan.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* https://www.logswan.org
*
* Created: 2015-05-31
* Last Updated: 2021-02-15
* Last Updated: 2021-11-16
*
* Logswan is released under the BSD 2-Clause license.
* See LICENSE file for details.
Expand Down Expand Up @@ -75,7 +75,8 @@ main(int argc, char *argv[])
int opt;

const char *errstr;
char linebuffer[LINE_LENGTH_MAX];
char *linebuffer = NULL;
size_t linesize = 0;
char *input;
char *db = NULL;

Expand Down Expand Up @@ -167,7 +168,7 @@ main(int argc, char *argv[])
results.file_name = input;
results.file_size = logfile_stat.st_size;

while (fgets(linebuffer, LINE_LENGTH_MAX, logfile)) {
while (getline(&linebuffer, &linesize, logfile) != -1) {
/* Parse and tokenize line */
parse_line(&parsed_line, linebuffer);

Expand Down Expand Up @@ -315,6 +316,7 @@ main(int argc, char *argv[])
fprintf(stderr, "Processed %" PRIu64 " lines in %f seconds.\n", results.processed_lines, results.runtime);

/* Clean up */
free(linebuffer);
fclose(logfile);

if (geoip)
Expand Down

0 comments on commit 7401e74

Please sign in to comment.