Changed code to use C++ functions, and select Panging Requests starting from message type byte - the effect will be probably shorter and easier to read code.
diff --git a/lib/misc_utils/tmsi_dumper_impl.cc b/lib/misc_utils/tmsi_dumper_impl.cc
index 10f06fe..adb6871 100644
--- a/lib/misc_utils/tmsi_dumper_impl.cc
+++ b/lib/misc_utils/tmsi_dumper_impl.cc
@@ -2,22 +2,22 @@
 /* @file
  * @author Piotr Krysik <ptrkrysik@gmail.com>
  * @section LICENSE
- * 
+ *
  * Gr-gsm is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 3, or (at your option)
  * any later version.
- * 
+ *
  * Gr-gsm is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with gr-gsm; see the file COPYING.  If not, write to
  * the Free Software Foundation, Inc., 51 Franklin Street,
  * Boston, MA 02110-1301, USA.
- * 
+ *
  */
 
 #ifdef HAVE_CONFIG_H
@@ -27,353 +27,159 @@
 #include <gnuradio/io_signature.h>
 #include "tmsi_dumper_impl.h"
 #include "grgsm/gsmtap.h"
-#include <stdio.h>
-#include <ctime>
-FILE* tmsiFile;
+#include <iostream>
+#include <boost/format.hpp>
 
 namespace gr {
-  namespace gsm {
-   
-    void tmsi_dumper_impl::dump_tmsi(pmt::pmt_t msg)
-    {
-	time_t now = time(0);
-	tm *ltm = localtime(&now);
-        pmt::pmt_t message_plus_header_blob = pmt::cdr(msg);
-        uint8_t * message_plus_header = (uint8_t *)pmt::blob_data(message_plus_header_blob);
-        size_t message_plus_header_len=pmt::blob_length(message_plus_header_blob);
-        
-        gsmtap_hdr * header = (gsmtap_hdr *)message_plus_header;
-        int ii=sizeof(gsmtap_hdr);
+namespace gsm {
 
-	switch(message_plus_header[ii]){
-	case 37:
-	  //1 tmsi 6-9
-
-	if((unsigned char)message_plus_header[ii+1] == 6 && (unsigned char)message_plus_header[ii+2] == 33)
-	{
-	if((unsigned char)message_plus_header[ii+6] != 43 && (unsigned char)message_plus_header[ii+6] != 43)
-	{
-	  //printf("INSIDE 37\n");
-	  tmsiFile = fopen("tmsicount.txt", "a+");
-	  fprintf(tmsiFile,"%02.2x%02.2x%02.2x%02.2x-%02i%02i%02i%02i%02i%02i-%i\n",
-		  (unsigned char)message_plus_header[ii+6],
-		  (unsigned char)message_plus_header[ii+7],
-		  (unsigned char)message_plus_header[ii+8],
-		  (unsigned char)message_plus_header[ii+9],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,
-				0);
-	  fclose(tmsiFile);
-	}
-	}
-	  break;
-	case 49:
-	   if(message_plus_header[ii+2] == 33)//21 hex
-	   {
-	  //printf("INSIDE 49\n");
-	  //1 imsi 5-12 33
-	  tmsiFile = fopen("tmsicount.txt", "a");
-	  fprintf(tmsiFile,"%i-%02i%02i%02i%02i%02i%02i-%02.2x%02.2x%02.2x%02.2x%02.2x%02.2x%02.2x%02.2x\n",0,
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,
-
-		  (unsigned char)message_plus_header[ii+5],	
-		  (unsigned char)message_plus_header[ii+6],
-		  (unsigned char)message_plus_header[ii+7],
-		  (unsigned char)message_plus_header[ii+8],
-		  (unsigned char)message_plus_header[ii+9],
-		  (unsigned char)message_plus_header[ii+10],
-		  (unsigned char)message_plus_header[ii+11],
-		  (unsigned char)message_plus_header[ii+12]);
-		  fclose(tmsiFile);
-	}
-	  break;
-	case 65:
-	  //printf("INSIDE 65\n"); (41 hex) (65 int) 6-8 13-16
-	   if(message_plus_header[ii+2] == 33)//21 hex
-	   {
-	      tmsiFile = fopen("tmsicount.txt", "a+");
-	      fprintf(tmsiFile,"%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n",
-		  (unsigned char)message_plus_header[ii+6],
-		  (unsigned char)message_plus_header[ii+7],
-		  (unsigned char)message_plus_header[ii+8],
-		  (unsigned char)message_plus_header[ii+9],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0,
-
-		  (unsigned char)message_plus_header[ii+13],
-		  (unsigned char)message_plus_header[ii+14],
-		  (unsigned char)message_plus_header[ii+15],
-		  (unsigned char)message_plus_header[ii+16],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0);
-		  fclose(tmsiFile);
-	   }
-	  break;
-	case 73:
-	  //printf("INSIDE 73\n");
-	  //3 tmsi 4-7 8-11 15-18
-	  if(message_plus_header[ii+2] == 34)
-	    {
-	  tmsiFile = fopen("tmsicount.txt", "a+");
-	  fprintf(tmsiFile,"%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n",
-		  (unsigned char)message_plus_header[ii+4],
-		  (unsigned char)message_plus_header[ii+5],
-		  (unsigned char)message_plus_header[ii+6],
-		  (unsigned char)message_plus_header[ii+7],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0,
-
-		  (unsigned char)message_plus_header[ii+8],
-		  (unsigned char)message_plus_header[ii+9],
-		  (unsigned char)message_plus_header[ii+10],
-		  (unsigned char)message_plus_header[ii+11],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0,
-
-		  (unsigned char)message_plus_header[ii+15],
-		  (unsigned char)message_plus_header[ii+16],
-		  (unsigned char)message_plus_header[ii+17],
-		  (unsigned char)message_plus_header[ii+18],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0);
-		  fclose(tmsiFile);
-	    }
-	  break;
-	case 77:
-	  if(message_plus_header[ii+2] == 33 && message_plus_header[ii+4] == 8)
-	  {
-	    //1 imsi 1 tmsi 5-12 16-19
-	  //printf("INSIDE 77 + 33 + 8\n");
-	  tmsiFile = fopen("tmsicount.txt", "a+");
-	  fprintf(tmsiFile,"%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%02.2x%02.2x%02.2x%02.2x%02.2x%02.2x%02.2x%02.2x\n",
-		  (unsigned char)message_plus_header[ii+16],
-		  (unsigned char)message_plus_header[ii+17],
-		  (unsigned char)message_plus_header[ii+18],
-		  (unsigned char)message_plus_header[ii+19],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,
-
-		  (unsigned char)message_plus_header[ii+5],	
-		  (unsigned char)message_plus_header[ii+6],
-		  (unsigned char)message_plus_header[ii+7],
-		  (unsigned char)message_plus_header[ii+8],
-		  (unsigned char)message_plus_header[ii+9],
-		  (unsigned char)message_plus_header[ii+10],
-		  (unsigned char)message_plus_header[ii+11],
-		  (unsigned char)message_plus_header[ii+12]);
-	   fclose(tmsiFile);
-	  }
-	  else if(message_plus_header[ii+2] == 33 && message_plus_header[ii+4] == 5)
-	  {
-	    //1 tmsi 1 imsi 6-9 12-19
-	  //printf("INSIDE 77 + 33 + 5\n");
-	  tmsiFile = fopen("tmsicount.txt", "a+");
-	  fprintf(tmsiFile,"%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%02.2x%02.2x%02.2x%02.2x%02.2x%02.2x%02.2x%02.2x\n",
-		  (unsigned char)message_plus_header[ii+6],
-		  (unsigned char)message_plus_header[ii+7],
-		  (unsigned char)message_plus_header[ii+8],
-		  (unsigned char)message_plus_header[ii+9],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,
-
-		  (unsigned char)message_plus_header[ii+12],	
-		  (unsigned char)message_plus_header[ii+13],
-		  (unsigned char)message_plus_header[ii+14],
-		  (unsigned char)message_plus_header[ii+15],
-		  (unsigned char)message_plus_header[ii+16],
-		  (unsigned char)message_plus_header[ii+17],
-		  (unsigned char)message_plus_header[ii+18],
-		  (unsigned char)message_plus_header[ii+19]);
-	   fclose(tmsiFile);
-	  }
-	  else if(message_plus_header[ii+2] == 34)
-	  {
-	  //printf("INSIDE 77 + 34\n");
-	  tmsiFile = fopen("tmsicount.txt", "a+");
-	  fprintf(tmsiFile,"%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n",
-		  (unsigned char)message_plus_header[ii+4],
-		  (unsigned char)message_plus_header[ii+5],
-		  (unsigned char)message_plus_header[ii+6],
-		  (unsigned char)message_plus_header[ii+7],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0,
-
-		  (unsigned char)message_plus_header[ii+8],
-		  (unsigned char)message_plus_header[ii+9],
-		  (unsigned char)message_plus_header[ii+10],
-		  (unsigned char)message_plus_header[ii+11],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0,
-
-		  (unsigned char)message_plus_header[ii+15],
-		  (unsigned char)message_plus_header[ii+16],
-		  (unsigned char)message_plus_header[ii+17],
-		  (unsigned char)message_plus_header[ii+18],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0);
-		  fclose(tmsiFile);
-	  }
-	  else if(message_plus_header[ii+2] == 36)
-	  {
-	  //printf("INSIDE 77 + 36\n");
-	  tmsiFile = fopen("tmsicount.txt", "a+");
-	  fprintf(tmsiFile,"%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n",
-		  (unsigned char)message_plus_header[ii+4],
-		  (unsigned char)message_plus_header[ii+5],
-		  (unsigned char)message_plus_header[ii+6],
-		  (unsigned char)message_plus_header[ii+7],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0,
-
-		  (unsigned char)message_plus_header[ii+8],
-		  (unsigned char)message_plus_header[ii+9],
-		  (unsigned char)message_plus_header[ii+10],
-		  (unsigned char)message_plus_header[ii+11],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0,
-
-		  (unsigned char)message_plus_header[ii+12],
-		  (unsigned char)message_plus_header[ii+13],
-		  (unsigned char)message_plus_header[ii+14],
-		  (unsigned char)message_plus_header[ii+15],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0,
-
-		  (unsigned char)message_plus_header[ii+16],
-		  (unsigned char)message_plus_header[ii+17],
-		  (unsigned char)message_plus_header[ii+18],
-		  (unsigned char)message_plus_header[ii+19],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0);
-		  fclose(tmsiFile);
-	  }
-	  break;
-	case 85:
-	  if(message_plus_header[ii+2] == 34)//hex 22
-	    {
-	      //printf("INSIDE 85\n");// hex is 55
-	      tmsiFile = fopen("tmsicount.txt", "a+");
-	      fprintf(tmsiFile,"%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n%02.2x%02.2x%02.2x%02.2x-%02d%02d%02d%02d%02d%02d-%i\n",
-		  (unsigned char)message_plus_header[ii+4],
-		  (unsigned char)message_plus_header[ii+5],
-		  (unsigned char)message_plus_header[ii+6],
-		  (unsigned char)message_plus_header[ii+7],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0,
-
-		  (unsigned char)message_plus_header[ii+8],
-		  (unsigned char)message_plus_header[ii+9],
-		  (unsigned char)message_plus_header[ii+10],
-		  (unsigned char)message_plus_header[ii+11],
-		   	   1900 + ltm->tm_year-2000,
-		   	   1+ltm->tm_mon,
-		   	   ltm->tm_mday,
-		   	   ltm->tm_hour,
-		   	   ltm->tm_min,
-		   	   ltm->tm_sec,0);
-		  fclose(tmsiFile);
-	    }
-	  break;
-
-	}
-
-        std::cout << std::endl;
-    }
+void tmsi_dumper_impl::dump_tmsi(pmt::pmt_t msg)
+{
+    time_t t = time(0);
+    tm *now = localtime(&t);
     
-    tmsi_dumper::sptr
-    tmsi_dumper::make()
-    {
-      return gnuradio::get_initial_sptr
-        (new tmsi_dumper_impl());
-    }
+    pmt::pmt_t message_plus_header_blob = pmt::cdr(msg);
+    uint8_t * message_plus_header = (uint8_t *)pmt::blob_data(message_plus_header_blob);
+    gsmtap_hdr * header = (gsmtap_hdr *)message_plus_header;
+    uint8_t * m = (uint8_t *)(message_plus_header+sizeof(gsmtap_hdr)); //message content
 
-    /*
-     * The private constructor
-     */
-    tmsi_dumper_impl::tmsi_dumper_impl()
-      : gr::block("tmsi_dumper",
-              gr::io_signature::make(0, 0, 0),
-              gr::io_signature::make(0, 0, 0))
-    {
-        message_port_register_in(pmt::mp("msgs"));
-        set_msg_handler(pmt::mp("msgs"), boost::bind(&tmsi_dumper_impl::dump_tmsi, this, _1));
-    }
+    uint8_t msg_len = m[0];
+    uint8_t direction_and_protocol = m[1];
+    uint8_t msg_type = m[2];
 
-    /*
-     * Our virtual destructor.
-     */
-    tmsi_dumper_impl::~tmsi_dumper_impl()
+    if( direction_and_protocol == 0x06 &&                    //direction from originating site, transaction id==0, Radio Resouce Management protocol
+        (msg_type==0x21 || msg_type==0x22 || msg_type==0x24) //types corresponding to paging requests
+      ) 
     {
+        switch(msg_type) {
+            case 0x21: //Paging Request Type 1
+            {
+                uint8_t mobile_identity_type = m[5] & 0x07;
+                unsigned int next_element = 0; //position of the next element
+                bool found_id_element = false;
+                                                
+                if(mobile_identity_type == 0x04) //identity type: TMSI
+                {
+                    write_tmsi(m+6);
+                    dump_file << "-";
+                    write_timestamp(now);
+                    dump_file << "-0";
+                    dump_file << std::endl;               
+
+                    next_element = 10;
+                    found_id_element = true;                    
+                } else 
+                if(mobile_identity_type == 0x01) //identity type: IMSI
+                {
+                    dump_file << "0-";
+                    write_timestamp(now);
+                    dump_file << "-";
+                    write_imsi(m+5);  
+                    dump_file << std::endl;                                      
+
+                    next_element = 13;
+                    found_id_element = true;
+                }
+                
+                if(found_id_element == true)
+                {
+                    //check if there is additional id element
+                    uint8_t element_id = m[next_element];
+                    if((next_element < (msg_len+1)) && (element_id == 0x17)){
+                        uint8_t element_len = m[next_element+1];
+                        mobile_identity_type = m[next_element+2] & 0x07;
+                        if(mobile_identity_type == 0x04) //identity type: TMSI
+                        {
+                            write_tmsi(m+next_element+3); //write starting from position of the TMSI in the message
+                            dump_file << "-";
+                            write_timestamp(now);
+                            dump_file << "-0";  
+                            dump_file << std::endl;
+                        } else 
+                        if(mobile_identity_type == 0x01) //identity type: IMSI
+                        {
+                            dump_file << "0-";
+                            write_timestamp(now);
+                            dump_file << "-";
+                            write_imsi(m+next_element+2); //write starting from position of the IMSI in the message
+                            dump_file << std::endl;
+                        }
+                    }
+                    int ii;
+                } 
+            }
+            break;
+            case 0x22: //Paging Request Type 2
+            break;
+            case 0x24: //Paging Request Type 3
+            break;
+        }
     }
-  } /* namespace gsm */
+}
+
+inline void tmsi_dumper_impl::write_timestamp(tm * now)
+{
+    dump_file << boost::format("%d%02d%02d%02d%02d%02d")
+               % (now->tm_year + 1900)                       //year
+               % (now->tm_mon + 1)                           //month
+               % now->tm_mday                                //day
+               % now->tm_hour % now->tm_min % now->tm_sec;  //time of day
+    return;
+}
+
+inline int swap(uint8_t c)
+{
+    uint8_t temp1, temp2;
+    temp1 = c & 0x0F;
+    temp2 = c & 0xF0;
+    temp1=temp1 << 4;
+    temp2=temp2 >> 4; 
+    return(temp2|temp1);
+}
+
+void tmsi_dumper_impl::write_imsi(uint8_t * imsi)
+{
+    dump_file << boost::format("%1x%02x%02x%02x%02x%02x%02x%02x")
+              % (swap(imsi[0]) & 0x0f)
+              % swap(imsi[1]) % swap(imsi[2]) % swap(imsi[3]) % swap(imsi[4]) % swap(imsi[5]) % swap(imsi[6]) % swap(imsi[7]);
+    return;
+}
+
+void tmsi_dumper_impl::write_tmsi(uint8_t * tmsi)
+{
+    dump_file << boost::format("%02x%02x%02x%02x")
+              % (int)tmsi[0] % (int)tmsi[1] % (int)tmsi[2] % (int)tmsi[3];
+    return;
+}
+
+
+tmsi_dumper::sptr
+tmsi_dumper::make()
+{
+    return gnuradio::get_initial_sptr
+           (new tmsi_dumper_impl());
+}
+
+/*
+ * The private constructor
+ */
+tmsi_dumper_impl::tmsi_dumper_impl()
+    : gr::block("tmsi_dumper",
+                gr::io_signature::make(0, 0, 0),
+                gr::io_signature::make(0, 0, 0))
+{
+    dump_file.open("tmsicount.txt", std::ios_base::out);
+    message_port_register_in(pmt::mp("msgs"));
+    set_msg_handler(pmt::mp("msgs"), boost::bind(&tmsi_dumper_impl::dump_tmsi, this, _1));
+}
+
+/*
+ * Our virtual destructor.
+ */
+tmsi_dumper_impl::~tmsi_dumper_impl()
+{
+    dump_file.close();
+}
+} /* namespace gsm */
 } /* namespace gr */
 
diff --git a/lib/misc_utils/tmsi_dumper_impl.h b/lib/misc_utils/tmsi_dumper_impl.h
index 759ccac..8b5a038 100644
--- a/lib/misc_utils/tmsi_dumper_impl.h
+++ b/lib/misc_utils/tmsi_dumper_impl.h
@@ -24,6 +24,8 @@
 #define INCLUDED_GSM_TMSI_DUMPER_IMPL_H
 
 #include <grgsm/misc_utils/tmsi_dumper.h>
+#include <fstream>
+#include <ctime>
 
 namespace gr {
   namespace gsm {
@@ -31,8 +33,11 @@
     class tmsi_dumper_impl : public tmsi_dumper
     {
      private:
+      std::ofstream dump_file;
       void dump_tmsi(pmt::pmt_t msg);
-
+      void write_timestamp(tm * now);
+      void write_imsi(uint8_t * imsi);
+      void write_tmsi(uint8_t * tmsi);
      public:
       tmsi_dumper_impl();
       ~tmsi_dumper_impl();