Patchwork [2/6] lib: fwts_log: add fwts_log_get_filenames()

login
register
mail settings
Submitter Colin King
Date June 20, 2012, 11:30 a.m.
Message ID <1340191829-27444-3-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/166018/
State Accepted
Headers show

Comments

Colin King - June 20, 2012, 11:30 a.m.
From: Colin Ian King <colin.king@canonical.com>

Add function to return back a string of all the log filenames being
used.  For multi-log logging, this returns multiple filenames
separated by a space, e.g. "results.log results.html results.json"

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/include/fwts_log.h |    1 +
 src/lib/src/fwts_log.c     |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
Keng-Yu Lin - June 20, 2012, 3 p.m.
On Wed, Jun 20, 2012 at 7:30 PM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Add function to return back a string of all the log filenames being
> used.  For multi-log logging, this returns multiple filenames
> separated by a space, e.g. "results.log results.html results.json"
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/include/fwts_log.h |    1 +
>  src/lib/src/fwts_log.c     |   40 ++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 41 insertions(+)
>
> diff --git a/src/lib/include/fwts_log.h b/src/lib/include/fwts_log.h
> index 513bf88..22ec70e 100644
> --- a/src/lib/include/fwts_log.h
> +++ b/src/lib/include/fwts_log.h
> @@ -144,6 +144,7 @@ int           fwts_log_line_number(fwts_log *log);
>  void     fwts_log_set_line_width(const int width);
>  void     fwts_log_section_begin(fwts_log *log, const char *name);
>  void     fwts_log_section_end(fwts_log *log);
> +char     *fwts_log_get_filenames(const char *filename, fwts_log_type type);
>  fwts_log_filename_type fwts_log_get_filename_type(const char *name);
>
>  static inline int fwts_log_type_count(fwts_log_type type)
> diff --git a/src/lib/src/fwts_log.c b/src/lib/src/fwts_log.c
> index 3ab7930..d946c31 100644
> --- a/src/lib/src/fwts_log.c
> +++ b/src/lib/src/fwts_log.c
> @@ -562,6 +562,46 @@ fwts_log_filename_type fwts_log_get_filename_type(const char *filename)
>  }
>
>  /*
> + *  fwts_log_filenames()
> + *     return string of all the log filenames that will be used
> + */
> +char *fwts_log_get_filenames(const char *filename, fwts_log_type type)
> +{
> +       unsigned int i;
> +       char *filenames = NULL;
> +       char *tmp;
> +       size_t len = 0;
> +
> +       for (i=0; i<32; i++) {
> +               fwts_log_type mask = 1 << i;
> +               if (type & mask) {
> +                       if ((tmp = fwts_log_filename(filename, mask)) == NULL)
> +                               return NULL;
> +
> +                       if (filenames) {
> +                               len += strlen(tmp) + 2;
> +                               if ((filenames = realloc(filenames, len)) == NULL) {
> +                                       free(tmp);
> +                                       return NULL;
> +                               }
> +                               strcat(filenames, " ");
> +                               strcat(filenames, tmp);
> +                       } else {
> +                               len = strlen(tmp) + 1;
> +                               if ((filenames = malloc(len)) == NULL) {
> +                                       free(tmp);
> +                                       return NULL;
> +                               }
> +                               strcpy(filenames, tmp);
> +                       }
> +                       free(tmp);
> +               }
> +       }
> +
> +       return filenames;
> +}
> +
> +/*
>  *  fwts_log_open()
>  *     open a log file. if name is stderr or stdout, then attach log to these
>  *     streams.
> --
> 1.7.10.4
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Alex Hung - June 21, 2012, 1:04 a.m.
On 06/20/2012 07:30 PM, Colin King wrote:
> From: Colin Ian King<colin.king@canonical.com>
>
> Add function to return back a string of all the log filenames being
> used.  For multi-log logging, this returns multiple filenames
> separated by a space, e.g. "results.log results.html results.json"
>
> Signed-off-by: Colin Ian King<colin.king@canonical.com>
> ---
>   src/lib/include/fwts_log.h |    1 +
>   src/lib/src/fwts_log.c     |   40 ++++++++++++++++++++++++++++++++++++++++
>   2 files changed, 41 insertions(+)
>
> diff --git a/src/lib/include/fwts_log.h b/src/lib/include/fwts_log.h
> index 513bf88..22ec70e 100644
> --- a/src/lib/include/fwts_log.h
> +++ b/src/lib/include/fwts_log.h
> @@ -144,6 +144,7 @@ int 	  fwts_log_line_number(fwts_log *log);
>   void	  fwts_log_set_line_width(const int width);
>   void	  fwts_log_section_begin(fwts_log *log, const char *name);
>   void	  fwts_log_section_end(fwts_log *log);
> +char     *fwts_log_get_filenames(const char *filename, fwts_log_type type);
>   fwts_log_filename_type fwts_log_get_filename_type(const char *name);
>
>   static inline int fwts_log_type_count(fwts_log_type type)
> diff --git a/src/lib/src/fwts_log.c b/src/lib/src/fwts_log.c
> index 3ab7930..d946c31 100644
> --- a/src/lib/src/fwts_log.c
> +++ b/src/lib/src/fwts_log.c
> @@ -562,6 +562,46 @@ fwts_log_filename_type fwts_log_get_filename_type(const char *filename)
>   }
>
>   /*
> + *  fwts_log_filenames()
> + *	return string of all the log filenames that will be used
> + */
> +char *fwts_log_get_filenames(const char *filename, fwts_log_type type)
> +{
> +	unsigned int i;
> +	char *filenames = NULL;
> +	char *tmp;
> +	size_t len = 0;
> +
> +	for (i=0; i<32; i++) {
> +		fwts_log_type mask = 1<<  i;
> +		if (type&  mask) {
> +			if ((tmp = fwts_log_filename(filename, mask)) == NULL)
> +				return NULL;
> +
> +			if (filenames) {
> +				len += strlen(tmp) + 2;
> +				if ((filenames = realloc(filenames, len)) == NULL) {
> +					free(tmp);
> +					return NULL;
> +				}
> +				strcat(filenames, " ");
> +				strcat(filenames, tmp);
> +			} else {
> +				len = strlen(tmp) + 1;
> +				if ((filenames = malloc(len)) == NULL) {
> +					free(tmp);
> +					return NULL;
> +				}
> +				strcpy(filenames, tmp);
> +			}
> +			free(tmp);
> +		}
> +	}
> +
> +	return filenames;
> +}
> +
> +/*
>    *  fwts_log_open()
>    *	open a log file. if name is stderr or stdout, then attach log to these
>    *	streams.
Acked-by: Alex Hung <alex.hung@canonical.com>

Patch

diff --git a/src/lib/include/fwts_log.h b/src/lib/include/fwts_log.h
index 513bf88..22ec70e 100644
--- a/src/lib/include/fwts_log.h
+++ b/src/lib/include/fwts_log.h
@@ -144,6 +144,7 @@  int 	  fwts_log_line_number(fwts_log *log);
 void	  fwts_log_set_line_width(const int width);
 void	  fwts_log_section_begin(fwts_log *log, const char *name);
 void	  fwts_log_section_end(fwts_log *log);
+char     *fwts_log_get_filenames(const char *filename, fwts_log_type type);
 fwts_log_filename_type fwts_log_get_filename_type(const char *name);
 
 static inline int fwts_log_type_count(fwts_log_type type)
diff --git a/src/lib/src/fwts_log.c b/src/lib/src/fwts_log.c
index 3ab7930..d946c31 100644
--- a/src/lib/src/fwts_log.c
+++ b/src/lib/src/fwts_log.c
@@ -562,6 +562,46 @@  fwts_log_filename_type fwts_log_get_filename_type(const char *filename)
 }
 
 /*
+ *  fwts_log_filenames()
+ *	return string of all the log filenames that will be used
+ */
+char *fwts_log_get_filenames(const char *filename, fwts_log_type type)
+{
+	unsigned int i;
+	char *filenames = NULL;
+	char *tmp;
+	size_t len = 0;
+
+	for (i=0; i<32; i++) {
+		fwts_log_type mask = 1 << i;
+		if (type & mask) {
+			if ((tmp = fwts_log_filename(filename, mask)) == NULL)
+				return NULL;
+
+			if (filenames) {
+				len += strlen(tmp) + 2;
+				if ((filenames = realloc(filenames, len)) == NULL) {
+					free(tmp);
+					return NULL;
+				}
+				strcat(filenames, " ");
+				strcat(filenames, tmp);
+			} else {
+				len = strlen(tmp) + 1;
+				if ((filenames = malloc(len)) == NULL) {
+					free(tmp);
+					return NULL;
+				}
+				strcpy(filenames, tmp);
+			}
+			free(tmp);
+		}
+	}
+
+	return filenames;
+}
+
+/*
  *  fwts_log_open()
  *	open a log file. if name is stderr or stdout, then attach log to these
  *	streams.